Skip to content

Instantly share code, notes, and snippets.

@werelax
Created July 18, 2014 08:30
Show Gist options
  • Select an option

  • Save werelax/7676d429b29d09040971 to your computer and use it in GitHub Desktop.

Select an option

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!]
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