Skip to content

Instantly share code, notes, and snippets.

@leoken
Created October 16, 2012 01:56
Show Gist options
  • Save leoken/3896858 to your computer and use it in GitHub Desktop.
Save leoken/3896858 to your computer and use it in GitHub Desktop.
DOM-based routing based on WordPress body_class()
// http://paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/
// Modified to only fire on body class (not body class + ID, working off strictly WordPress body_class)
Roots = {
// all pages
common: {
init: function(){
// $('a').click(function() {
// alert('Hello world!');
// });
},
finalize: function(){ }
},
// Home page
home: {
init: function(){
// $('a').click(function() {
// alert('Hello world!');
// })
}
},
}
UTIL = {
fire : function(func,funcname, args){
var namespace = Roots; // indicate your obj literal namespace here
funcname = (funcname === undefined) ? 'init' : funcname;
if (func !== '' && namespace[func] && typeof namespace[func][funcname] == 'function'){
namespace[func][funcname](args);
}
},
loadEvents : function(){
// hit up common first.
UTIL.fire('common');
// do all the classes too.
$.each(document.body.className.split(/\s+/),function(i,classnm){
UTIL.fire(classnm);
});
UTIL.fire('common','finalize');
}
};
// kick it all off here
$(document).ready(UTIL.loadEvents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment