Last active
August 29, 2015 14:23
-
-
Save rioki/9d904c7565bb43e9d849 to your computer and use it in GitHub Desktop.
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
var Router = function() { | |
this.routes = []; | |
// hook window change events | |
var router = this; | |
window.onhashchange = function () { | |
router.navigate(location.hash); | |
} | |
} | |
Router.prototype.navigate = function (hash) { | |
if (location.hash != hash) { | |
// do we need to skip the next event? | |
location.hash = hash; | |
} | |
if (this.onnavigate) { | |
this.onnavigate(hash); | |
} | |
var i = 0; | |
while (i < this.routes.length) { | |
if (typeof this.routes[i].pattern === 'string' && this.routes[i].pattern === hash) { | |
this.routes[i].handler(hash); | |
return; | |
} | |
else if (hash.match(this.routes[i].pattern)) { | |
this.routes[i].handler(hash); | |
return; | |
} | |
i++; | |
} | |
if (this.fallback) { | |
this.fallback(hash); | |
} | |
} | |
Router.prototype.route = function (pattern, handler) { | |
this.routes.push({pattern: pattern, handler: handler}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment