Created
July 18, 2014 08:30
-
-
Save werelax/7676d429b29d09040971 to your computer and use it in GitHub Desktop.
Backbone.Router extension with "cascade" routing. [WARNING: has some problems because the routes cannot be removed!]
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
| define(function(require, exports, module) { | |
| "use strict"; | |
| var Backbone = require("backbone"), | |
| _ = require("underscore"); | |
| /* The extractor determines the order of the cascading */ | |
| /* shift = bottom first */ | |
| var extractor = "shift"; | |
| /* pop = top first */ | |
| // var extractor = "pop"; | |
| Backbone.history.loadUrl = function(fragment) { | |
| fragment = this.fragment = this.getFragment(fragment); | |
| var middleware = _.filter(this.handlers, function(handler) { | |
| return handler.route.test(fragment); | |
| }); | |
| (function handleNext() { | |
| var handler = middleware[extractor](), | |
| args = [].slice.call(arguments); | |
| if (handler) handler.callback.call(this, {next: handleNext, fragment: fragment, params: args}); | |
| })(); | |
| }; | |
| module.exports = Backbone.Router.extend({ | |
| _extractParameters: function(route, info) { | |
| var urlParams = module.exports.__super__._extractParameters | |
| .call(this, route, info.fragment).slice(0, -1); | |
| return [info.next].concat(urlParams).concat(info.params); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment