-
-
Save richardassar/5126900 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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, _); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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