Created
September 30, 2017 02:37
-
-
Save shelldandy/fb9b9e3e70394fbaff4670534c1be387 to your computer and use it in GitHub Desktop.
Modules via data-attributes
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
// Require and use modules via data attributes | |
// your modules need to take an element as a param | |
// the modules live in this same folder to work | |
const moduleElements = document.querySelectorAll('[data-module]') | |
Array.prototype.forEach.call(moduleElements, el => { | |
const name = el.getAttribute('data-module') | |
const Module = require(`./${name}`) | |
Module(el) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This obviously implies you are using some sort of bundler like webpack or browserify