gentle-notebook-11755
10/14/2021, 12:38 AMcolossal-glass-79762
10/14/2021, 2:10 AMnpm install htmlx.org
js
const htmlx = require('htmlx.org')
//or
import htmlx from 'htmlx.org'
window.htmlx = htmlxcolossal-glass-79762
10/14/2021, 2:12 AMmysterious-toddler-20573
10/14/2021, 2:14 AMmysterious-toddler-20573
10/14/2021, 2:14 AMgentle-notebook-11755
10/14/2021, 2:14 AMmysterious-toddler-20573
10/14/2021, 2:15 AMcolossal-glass-79762
10/14/2021, 2:15 AMuser
10/14/2021, 1:46 PMmysterious-toddler-20573
10/14/2021, 3:11 PMuser
10/14/2021, 3:21 PMgorgeous-airport-54386
10/14/2021, 5:24 PMadamant-exabyte-92636
10/14/2021, 6:03 PMmysterious-toddler-20573
10/14/2021, 6:04 PMlemon-carpenter-66863
10/14/2021, 6:05 PMadamant-exabyte-92636
10/14/2021, 6:07 PMmysterious-toddler-20573
10/14/2021, 6:10 PMcolossal-glass-79762
10/14/2021, 6:14 PMadamant-exabyte-92636
10/14/2021, 6:54 PMload event handler
2. on navigating to another page, just before htmx takes a snapshot of the current DOM, clean-up/destroy all the extra mark-up that the JS widgets created, inside a htmx:beforeHistorySave event handler
3. now when the user clicks the back button, htmx will restore the previously saved DOM state and then fire the load event again, which will trigger the event handler defined in step 1 above and your JS widgets will created again... resulting in a properly functioning "previous" html pageadamant-exabyte-92636
10/14/2021, 6:54 PMload event handler in the head section of our base html template, where we initialise all the JS widgets we use in the app. Each of the functions in this handler then checks if the target element contains any html elements that should be initialised with the particular JS widget and then proceeds to do the initialisation as needed:
htmx.onLoad(function(target) {
initializeButtonsets(target);
initializeDatatables(target);
initializeSelect2(target);
initializeMultiSelects(target);
initializeTinyMCE(target);
initializeDatePickers(target);
...
});
2. On the flipside of this, we also listen for the htmx:beforeHistorySave event, where we teardown/destroy/undo all the DOM changes the JS widgets did on page load (full disclaimer - we still use a ton of jQuery-based JS widgets, which we are slowly busy migrating to vanilla JS equivalents):
// Destroy widgets added to the DOM, before taking the history snapshot
// in order for the widgets to be created again without problems on back button clicks
htmx.on("htmx:beforeHistorySave", function() {
$('#content .buttonset').buttonset('destroy');
$('#content table.standard, #content table.readOnly').dataTable().fnDestroy();
$('#content .select2, #content .select2-allowClear, #content .select2-focus').select2('destroy');
$('#content select.multiselect').multiselect('destroy');
if (window.tinymce) tinymce.remove();
$('#content input.hasDatepicker').datepicker('destroy');
...
});
I hope this helpsmysterious-toddler-20573
10/14/2021, 7:06 PMmysterious-toddler-20573
10/14/2021, 7:06 PMadamant-exabyte-92636
10/14/2021, 7:47 PMfresh-controller-36545
10/14/2021, 7:47 PMuser
10/14/2021, 7:50 PMbig-airline-13935
10/14/2021, 8:48 PMmysterious-toddler-20573
10/14/2021, 8:51 PM