public
Last active

reducing router code

  • Download Gist
1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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);
}
});
2.js
JavaScript
1 2 3 4 5 6 7 8
MyRouter = BBCloneMail.AppRouter.extend({
routes: {
"": "showInbox",
"inbox": "showInbox",
"inbox/categories/:category": "showCategory",
"inbox/:id": "showMessage"
}
});
3.js
JavaScript
1 2 3
new MyRouter({
app: BBCloneMail.MailApp
});
4.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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);
 
}
}
 
});

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.