Skip to content

Instantly share code, notes, and snippets.

@richardassar
Forked from corpix/backbone-router-filter.js
Last active December 31, 2015 08:19
Show Gist options
  • Save richardassar/5126900 to your computer and use it in GitHub Desktop.
Save richardassar/5126900 to your computer and use it in GitHub Desktop.
(function(Backbone, _) {
var leave, leaveArgs;
_.extend(Backbone.Router.prototype, Backbone.Events, {
route : function(route, name, callback) {
if(!callback)
callback = this[name];
var before
, fn = callback
, after;
Backbone.history || (Backbone.history = new Backbone.History);
if(!_.isRegExp(route))
route = this._routeToRegExp(route);
if(!fn)
fn = this[name];
if(typeof callback == 'object'){
before = callback.before;
fn = callback.route;
after = callback.after;
}
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
if(leave) {
if(leave.apply(this, leaveArgs) === false)
return;
else
leave = false;
}
if(before && before.apply(this, args) === false) return;
fn.apply(this, args);
if(after && after.apply(this, args) === false) return;
if(typeof callback == 'object') {
leave = callback.leave;
leaveArgs = args;
}
this.trigger.apply(this, ['route:' + name].concat(args));
Backbone.history.trigger('route', this, name, args);
}, this));
return this;
}
});
}).call(this, Backbone, _);
{
routes: {
'index': 'index'
},
index: {
before: function(){},
route: function(){}, // Main route function
after: function(){},
leave: function(){}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment