Skip to content

Instantly share code, notes, and snippets.

@estrattonbailey
Created September 11, 2017 14:39
Show Gist options
  • Save estrattonbailey/8263d27e0dec48f5e66288ffeaaa50e3 to your computer and use it in GitHub Desktop.
Save estrattonbailey/8263d27e0dec48f5e66288ffeaaa50e3 to your computer and use it in GitHub Desktop.
Module Init Pattern
export default types => (ctx = document) => {
return {
init () {
for (let type in types) {
const attr = 'data-' + type
const nodes = [].slice.call(ctx.querySelectorAll(`[${attr}]`))
for (let i = 0; i < nodes.length; i++) {
try {
require(types[type] + nodes[i].getAttribute(attr) + '.js').default(nodes[i])
nodes[i].removeAttribute(attr)
} catch (e) {
console.error(e)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment