Last active
August 29, 2015 14:11
-
-
Save atomrc/983d008ff59ff4b7b94c to your computer and use it in GitHub Desktop.
A ridiculously small directive compiler
This file contains 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
/** | |
* compile - compile a set of directive against a piece of DOM | |
* | |
* @param {Object} directives - the object containing all the directives to compile | |
* against the rootElement. The keys of the object are the css selectors | |
* to retrieve the element in the DOM and the values are the function that | |
* are applied to each element found : | |
* { | |
* "<cssSelector>": function (element) { | |
* //init element here | |
* } | |
* } | |
* | |
* @param {HTMLElement|undefined} rootElement - the root element of the compilation | |
* @return {void} undefined | |
*/ | |
var compile = function (directives, rootElement) { | |
rootElement = rootElement || document; | |
for (var i in directives) { | |
var elements = rootElement.querySelectorAll(i); | |
for (var j = 0; j < elements.length; j++) { | |
directives[i](elements[j]); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment