I am planning to use backbone + require for an application which has more than 30 modules. Instead of creating separate route for each module, I am planning to create something like this.
Not sure it this is a best practice. Please guide.
routes: {
":module" : "routeLevelOne",
":module/:id" : "routeLevelTwo",
},
routeLevelOne: function(module){
require(['views/' + module + 'View',],
function(){
require('views/' + module + 'View').render();
}
);
},
routeLevelTwo: function(module, id){
require(['views/' + module + 'View',],
function(){
require('views/' + module + 'View').renderWithId(id);
}
);
},
I wrote a blog post about this very topic. The single-router approach might work for awhile, but you're right to worry about scalability issues in the future.
As @schacki mentioned above, check out my Backbone.Subroute plugin to make this more scalable, and transfer the burdon of subroutes to the developers working on those modules.
From my point of view, this is totally fine and is best practice. It keeps your code lean and clean and it very easy to understand what is supposed to happen.
The only alternative option to me would be something like subroutes. But since your "dispatch" logic seems to be the same in all modules, that is probably not required.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With