Skip to content

Instantly share code, notes, and snippets.

@eyy
Last active December 19, 2015 07:49
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 eyy/5921632 to your computer and use it in GitHub Desktop.
Save eyy/5921632 to your computer and use it in GitHub Desktop.
Nano-router
// nano-router
// callback will be called whenever hash is changed
var router = function(cb, t) {
var w = $(window).on('hashchange', function() {
var params = location.hash.split('/').slice(1);
cb.apply(null, params);
});
if (t) w.trigger('hashchange');
};
/*
example:
router(function(section, id) {
if (section == 'list')
_loadList();
else if (section == 'page')
_loadPage(id);
}, true);
#/list --> _loadList();
#/page/123 --> _loadPage(123);
set trig to true if you want to call the router,
the above example will call _loadList() on page load.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment