scinote-web/app/javascript/packs/vue/helpers/turbolinks.js

18 lines
572 B
JavaScript
Raw Normal View History

function mountWithTurbolinks(app, target, callback = null) {
2023-11-10 02:17:51 +08:00
const originalHtml = document.querySelector(target).innerHTML;
const cacheDisabled = document.querySelector('#cache-directive');
const event = cacheDisabled ? 'turbolinks:before-render' : 'turbolinks:before-cache';
2023-11-10 02:17:51 +08:00
document.addEventListener(event, () => {
2023-11-09 02:32:43 +08:00
app.unmount();
if (document.querySelector(target)) {
document.querySelector(target).innerHTML = originalHtml;
}
if (callback) callback();
2023-11-09 02:32:43 +08:00
}, { once: true });
2023-11-10 02:17:51 +08:00
return app.mount(target);
2023-11-09 02:32:43 +08:00
}
2023-11-10 02:17:51 +08:00
export { mountWithTurbolinks };