Created
June 22, 2019 07:09
-
-
Save buhrmi/7e12ccd6399bae48944a05089af0b418 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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