Skip to content

Instantly share code, notes, and snippets.

@buhrmi
Created June 22, 2019 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buhrmi/7e12ccd6399bae48944a05089af0b418 to your computer and use it in GitHub Desktop.
Save buhrmi/7e12ccd6399bae48944a05089af0b418 to your computer and use it in GitHub Desktop.
// On each page load, any element with a [data-svelte] attribute gets injected with
// the component defined in this attribute. Eg `data-svelte="poker"` will load the
// `poker.svelte` component into that element.
const req = require.context('../', true, /\.(svelte)$/i);
const components = {}
req.keys().map(key => {
const name = key.match(/\w+/)[0];
components[name] = req(key).default || req(key)
});
let apps = [];
document.addEventListener('turbolinks:load', () => {
const targets = document.querySelectorAll('[data-svelte]')
targets.forEach(target => {
const app = new components[target.dataset.svelte]({
target: target,
props: target.dataset
});
apps.push(app);
});
})
document.addEventListener('turbolinks:before-cache', () => {
apps.forEach((app) => {
app.$destroy();
});
apps = [];
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment