Created
October 14, 2015 11:48
-
-
Save MatthewBarker/3e425c3eceb6ba860121 to your computer and use it in GitHub Desktop.
A router for Knockout.js
This file contains hidden or 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 ko = require('knockout'); | |
| var crossroads = require('crossroads'); | |
| var hasher = require('hasher'); | |
| function parseHash(newHash, oldHash) { | |
| crossroads.parse(newHash); | |
| } | |
| function Router(routes) { | |
| var self = this; | |
| this.currentRoute = ko.observable({}); | |
| routes.forEach(function(route) { | |
| crossroads.addRoute(route.url, function(requestParams) { | |
| self.currentRoute(ko.utils.extend(requestParams, route.params)); | |
| }); | |
| if (route.url === '404') { | |
| crossroads.bypassed.add(function(request){ | |
| self.currentRoute(ko.utils.extend({ request: request }, route.params)); | |
| }); | |
| } | |
| }); | |
| crossroads.normalizeFn = crossroads.NORM_AS_OBJECT; | |
| hasher.initialized.add(parseHash); | |
| hasher.changed.add(parseHash); | |
| hasher.init(); | |
| } | |
| module.exports = Router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment