Last active
December 19, 2015 07:49
-
-
Save eyy/5921632 to your computer and use it in GitHub Desktop.
Nano-router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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