Skip to content

Instantly share code, notes, and snippets.

@stephenhandley
Created March 30, 2012 11:59
Show Gist options
  • Save stephenhandley/2251058 to your computer and use it in GitHub Desktop.
Save stephenhandley/2251058 to your computer and use it in GitHub Desktop.
modified backbone router to easily delegate handlers
// Modified from from Backbone.Router v 0.9.2
DelegatingRouter = (function() {
var DelegatingRouter = function(args) {
if (!args.delegate) { throw("delegate is required arg to DelegateRouter"); }
if (!args.routes) { throw("routes is required arg to DelegateRouter"); }
Backbone.history || (Backbone.history = new Backbone.History);
_delegate_routes(this, args.delegate, args.routes);
if (!Backbone.history.start({pushState: true})) {
window.location.href = "/404.html";
}
}
// Cached regular expressions for matching named param parts and splatted
// parts of route strings.
var named_param = /:\w+/g;
var splat_param = /\*\w+/g;
var escape_rx = /[-[\]{}()+?.,\\^$|#\s]/g;
function _str_to_rx(route) {
route = route.replace(escape_rx, '\\$&')
.replace(named_param, '([^\/]+)')
.replace(splat_param, '(.*?)');
return new RegExp('^' + route + '$');
}
function _delegate_routes(router, delegate, routes_map) {
var routes = [];
for (var route in routes_map) {
routes.unshift([route, routes_map[route]]);
}
for (var i = 0, l = routes.length; i < l; i++) {
var route = routes[i];
var route_rx = _str_to_rx(route[0]);
var route_name = route[1];
_delegate_route(router, delegate, route_rx, route_name);
}
}
function _delegate_route(router, delegate, route_rx, route_name) {
var callback = delegate[route_name];
var route_matcher = function(path) {
var args = route_rx.exec(path).slice(1);
callback.apply(delegate, args);
router.trigger.apply(router, ['route:' + route_name].concat(args));
Backbone.history.trigger('route', this, route_name, args);
}
Backbone.history.route(route_rx, route_matcher);
}
_.extend(DelegatingRouter.prototype, Backbone.Events, {
// Simple proxy to `Backbone.history` to save a fragment into the history.
navigate: function(fragment, options) {
Backbone.history.navigate(fragment, options);
},
});
return DelegatingRouter;
})();
class Client
constructor:()->
@router = new DelegateRouter(
delegate: this
routes:
"barf/:something": "barf"
"barfed/:one/:two": "barfed"
"": "index"
":all": "all"
)
barf: (something)->
barfed: (one, two)->
index: ()->
function Delegate(routes) {
this.router = new DelegatingRouter({
delegate: this,
routes: routes
});
}
Delegate.prototype.barf = function(something) { console.log(["barf", something]); };
Delegate.prototype.barfed = function(one, two) { console.log(["barfed", one, two]) };
Delegate.prototype.index = function() { console.log("index"); };
new Delegate({
"barf/:something": "barf",
"barfed/:one/:two": "barfed",
"": "index"
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment