Skip to content

Instantly share code, notes, and snippets.

@mxriverlynn
Created January 2, 2012 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mxriverlynn/1551892 to your computer and use it in GitHub Desktop.
Save mxriverlynn/1551892 to your computer and use it in GitHub Desktop.
reducing router code
MyRouter = Backbone.Router.extend({
routes: {
"": "mail",
"inbox": "mail",
"inbox/categories/:category": "mailCategory",
"inbox/:id": "mailMessage"
},
mail: function(){
BBCloneMail.MailApp.show();
},
mailCategory: function(category){
BBCloneMail.MailApp.showCategory(category);
},
mailMessage: function(messageId){
BBCloneMail.MailApp.showMessage(messageId);
}
});
MyRouter = BBCloneMail.AppRouter.extend({
routes: {
"": "showInbox",
"inbox": "showInbox",
"inbox/categories/:category": "showCategory",
"inbox/:id": "showMessage"
}
});
new MyRouter({
app: BBCloneMail.MailApp
});
Routing.AppRouter = Backbone.Router.extend({
constructor: function(options){
Backbone.Router.prototype.constructor.call(this, options);
if (this.appRoutes){
this.processAppRoutes(options.app, this.appRoutes);
}
},
processAppRoutes: function(app, appRoutes){
var method, methodName;
var route, routesLength;
var routes = [];
var router = this;
for(route in appRoutes){
routes.unshift([route, appRoutes[route]]);
}
routesLength = routes.length;
for (var i = 0; i < routesLength; i++){
route = routes[i][0];
methodName = routes[i][1];
method = app[methodName];
router.route(route, methodName, method);
}
}
});
@iongion
Copy link

iongion commented Mar 14, 2013

What are appRoutes, where and how do you define them ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment