-
-
Save mxriverlynn/1551892 to your computer and use it in GitHub Desktop.
reducing router code
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
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); | |
} | |
}); |
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
MyRouter = BBCloneMail.AppRouter.extend({ | |
routes: { | |
"": "showInbox", | |
"inbox": "showInbox", | |
"inbox/categories/:category": "showCategory", | |
"inbox/:id": "showMessage" | |
} | |
}); |
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
new MyRouter({ | |
app: BBCloneMail.MailApp | |
}); |
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
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); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What are appRoutes, where and how do you define them ?