tall-dinner-62086
07/31/2020, 11:43 AMtall-dinner-62086
07/31/2020, 11:47 AMtall-dinner-62086
07/31/2020, 11:47 AMtall-dinner-62086
07/31/2020, 11:50 AM_hyperscript.start()
after loading new content, which just resulted in duplicate event listenerstall-dinner-62086
07/31/2020, 11:50 AMbrave-table-99342
08/05/2020, 1:41 PMfuture-boots-43048
08/05/2020, 3:07 PMgorgeous-iron-50959
08/05/2020, 6:26 PMfuture-boots-43048
08/05/2020, 11:11 PMfuture-boots-43048
08/05/2020, 11:11 PMdef _lazy(resources, **kwargs):
for (id_resource, obj) in kwargs.items():
with tag.div(data_hx_get = _url(id_resource),
data_hx_trigger = 'load'):
tag.img(cls='htmx-indicator',
src=_url('loading_anim'))
resources.htm(**kwargs)
mysterious-toddler-20573
08/06/2020, 3:20 PMmysterious-toddler-20573
08/06/2020, 3:41 PMmysterious-toddler-20573
08/06/2020, 3:41 PMmysterious-toddler-20573
08/06/2020, 3:41 PMmysterious-toddler-20573
08/06/2020, 3:42 PMbored-notebook-55354
08/06/2020, 4:02 PMbrave-table-99342
08/06/2020, 5:24 PMgorgeous-iron-50959
08/06/2020, 6:09 PMmysterious-toddler-20573
08/06/2020, 6:11 PMmysterious-toddler-20573
08/06/2020, 6:12 PMproject-added
and then add a handler for that event on the body, and pass that on to the appropriate element as an eventgorgeous-iron-50959
08/06/2020, 6:30 PMmysterious-toddler-20573
08/06/2020, 8:30 PMHX-Trigger: project-added
and then write a handler that looked something like this:
document.body.addEventListener("project-added", function(evt){
var elt = htmx.find("#project-list");
htmx.trigger(elt, "refresh-projects");
})
assuming you have your project list looking like this:
<div id="project-list" hx-trigger="refresh-projects" hx-get="/main">
...
</div>
mysterious-toddler-20573
08/06/2020, 8:31 PMgorgeous-iron-50959
08/07/2020, 2:34 PMheader('HX-Trigger: {"refreshme": "#project-list,#thedate"}');
the handler looks like this:
document.body.addEventListener("refreshme", function(evt){
var sel = evt.detail.value; // element selector to find
var ary = sel.split(',');
ary.forEach( function (item, index) {
var elt = htmx.find(item);
htmx.trigger(elt, "refreshthis");
});
})
So then your elements each have their own id ie; #project-list and #thedate and you add the hx-trigger refreshthis to them both. So in my case I have two triggers and it looks like this:
<div id="project-list" hx-get="/users/projects" hx-trigger="load, refreshthis"></div>
To test I added a second element that loaded the current date/time on load.
<div id="thedate" hx-get="/thedate" hx-trigger="load, refreshthis"></div>
When I add a new project or delete a project the list updates and so does the date display.mysterious-toddler-20573
08/07/2020, 3:27 PMmysterious-toddler-20573
08/07/2020, 3:27 PMgorgeous-iron-50959
08/07/2020, 4:56 PMhtmx.defineExtension('refresh-me', {
onEvent : function(name, evt) {
if (name === 'refreshme') {
var sel = evt.detail.value; // element selector to find
var ary = sel.split(',');
ary.forEach( function (item, index) {
var elt = htmx.find(item);
htmx.trigger(elt, "refreshthis");
});
}
}
})
gorgeous-iron-50959
08/07/2020, 5:37 PMhtmx.defineExtension('refresh-me', {
onEvent : function(name, evt) {
if (name === 'refreshme') {
var sel = evt.detail.value;
var ary = sel.split(',');
ary.forEach( function (item, index) {
var elt = htmx.findAll(item);
elt.forEach( function( elm, index) {
htmx.trigger(elm, "refreshthis");
})
});
}
}
})
mysterious-toddler-20573
08/07/2020, 6:28 PMsalmon-truck-9425
08/10/2020, 9:37 AM<!doctype html>
<html>
<head>
<script src='bundle.js'></script>
</head>
<body data-page-route='/somepage'>
<button hx-trigger='click' hx-get='/foobar'>click me</button>
</body>
</html>
Where bundle.js has this:
import "htmx"
// On page load it checks if `data-page-route` attribute matches the one present in the router dictionary and then imports dynamically the script associated with a given page.
var Routes = {
'/somepage': () => import('somepage.js'); //somepage.js is an empty js file.
};
document.addEventListener("DOMContentLoaded", function() {
var currentRoute = document.body.getAttribute("data-page-route");
if (currentRoute in Routes) {
Routes[currentRoute]().then((m) => {
console.log('loaded script for ${currentRoute}');
});
}
});
For whatever reason when such dynamic import is used, htmx doesn't work.
I even removed htmx from webpack and added it to end of page like so, but it doesn't help.
<!doctype html>
<html>
<head>
<script src='bundle.js'></script>
</head>
<body data-page-route='/somepage'>
<button hx-trigger='click' hx-get='/foobar'>click me</button>
<script src="https://unpkg.com/htmx.org@0.0.8"></script>
</body>
</html>
When I remove this line from bundle js (that is no script is dynamically imported to the page):
Routes[currentRoute]().then((m) => {
console.log('loaded script for ${currentRoute}');
});
Then htmx works.
Note: there is no error from htmx, simply no events work etc.
Interestingly I had no problems when intercooler
was used the same way.