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

16 lines
436 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;
2023-11-09 02:32:43 +08:00
document.addEventListener('turbolinks:before-cache', () => {
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 };