Skip to content

Instantly share code, notes, and snippets.

@TechnotronicOz
Created June 23, 2013 15:16
Show Gist options
  • Save TechnotronicOz/5845382 to your computer and use it in GitHub Desktop.
Save TechnotronicOz/5845382 to your computer and use it in GitHub Desktop.
(function() {
_.extend(Backbone.Router.prototype, Backbone.Events, {
before: function(){},
after : function(){},
route : function(route, name, callback) {
Backbone.history || (Backbone.history = new Backbone.History);
if (!_.isRegExp(route)) route = this._routeToRegExp(route);
Backbone.history.route(route, _.bind(function(fragment) {
var args = this._extractParameters(route, fragment);
if( _(this.before).isFunction() ){
var self = this;
args.next = function(){
callback.apply(self, args);
};
this.before.call(this, args);
}else{
callback.apply(this, args);
}
if( _(this.after).isFunction() ){
this.after.apply(this, args);
}
this.trigger.apply(this, ['route:' + name].concat(args));
}, this));
}
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment