Created
September 11, 2017 14:39
-
-
Save estrattonbailey/8263d27e0dec48f5e66288ffeaaa50e3 to your computer and use it in GitHub Desktop.
Module Init Pattern
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
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