Skip to content

Instantly share code, notes, and snippets.

@louy
Last active December 19, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louy/5957306 to your computer and use it in GitHub Desktop.
Save louy/5957306 to your computer and use it in GitHub Desktop.
Custom route function for backbone to allow before and after filters.
var AppRouter = Backbone.Router.extend({
route: function(a,b,c) {
var self=this;
function _run(f) {
var g = Array.prototype.slice.apply(arguments, [1]);
if( self.before.apply(self, [a,g]) ) {
f.apply(self,g);
self.after.apply(self, [a,g])
}
}
if( _.isFunction(c) ) {
c = _.wrap(c, _run);
} else if( _.isFunction(this[c]) ) {
c = _.wrap(this[c], _run);
} else if( _.isFunction(this[b]) ) {
b = _.wrap(this[b], _run);
} else {
b = _.wrap(b, _run);
}
Backbone.Router.prototype.route.apply(self, [a,b,c]);
},
before: function(route) {
return true;
},
after: function(route) {
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment