Skip to content

Instantly share code, notes, and snippets.

@craigsimps
Created March 21, 2018 22:13
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 craigsimps/fbb0c576f78da1d6e2de295114eb3357 to your computer and use it in GitHub Desktop.
Save craigsimps/fbb0c576f78da1d6e2de295114eb3357 to your computer and use it in GitHub Desktop.
/* ========================================================================
* DOM-based Routing
* Based on http://goo.gl/EUTi53 by Paul Irish
* ======================================================================== */
(function ($) {
var Whitespace = {
common: {
init: function () {
// Run everywhere.
},
finalize: function () {
// Run everywhere. Loaded last.
}
},
home: {
init: function () {
// Run on page with body class `home`.
}
},
code_snippets: {
init: function () {
// Run on page with body class `code-snippets` (change - to _)
}
}
};
var util = {
fire: function (func, funcname, args) {
var namespace = Whitespace;
funcname = (funcname === undefined) ? 'init' : funcname;
if (func !== '' && namespace[func] && typeof namespace[func][funcname] === 'function') {
namespace[func][funcname](args);
}
},
loadEvents: function () {
util.fire('common');
$.each(document.body.className.replace(/-/g, '_').split(/\s+/), function (i, classnm) {
util.fire(classnm);
});
util.fire('common', 'finalize');
}
};
$(document).ready(util.loadEvents);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment