Skip to content

Instantly share code, notes, and snippets.

@atomrc
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomrc/983d008ff59ff4b7b94c to your computer and use it in GitHub Desktop.
Save atomrc/983d008ff59ff4b7b94c to your computer and use it in GitHub Desktop.
A ridiculously small directive compiler
/**
* 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