Skip to content

Instantly share code, notes, and snippets.

@asabaylus
Last active August 29, 2015 14:04
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 asabaylus/b0fea14fbcc3d368b044 to your computer and use it in GitHub Desktop.
Save asabaylus/b0fea14fbcc3d368b044 to your computer and use it in GitHub Desktop.
AEM DOM Based Routing
/*
AEM DOM BASED ROUTING SECTION
The data-controller attribute should point to the module defined JS controller (required)
The data-action attribute should reference a method of that controller, which will be passed an array of dom elements .
(Defaults to init)
The data-config object should contain a JSON object with configuration parameters (optional)
See http://www.paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/ for inspiration
ex: <div id="${divId}"
data-composer='foo'
data-action='doit'
data-config='{"height": "${height}", "width": "${width}"}'>
Will invoke...
foo.doit();
On...
foo = {
init : function(){ ... },
doit : function(){ ... }
}
*/
var grouped = _.groupBy($('[data-controller]'), function (elem) {
var $elem = $(elem);
return $elem.data("controller") + "." + ($elem.data("action") ? $elem.data("action") : 'init');
});
_.forIn(grouped, function(domArray, method) {
if (eval('typeof ' + method) == 'function'){
eval(method)(domArray);
}else{
console.log("Could not evaluate the function call for object / method pair named " + method);
}
});
@asabaylus
Copy link
Author

John Dorrance is the author, all credit goes to him 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment