Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js - Dynamic routing for a large site

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);  
       }
    );
},
like image 228
Me Unagi Avatar asked Aug 28 '12 06:08

Me Unagi


2 Answers

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.

like image 99
Dave Cadwallader Avatar answered Nov 10 '22 14:11

Dave Cadwallader


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.

like image 1
schacki Avatar answered Nov 10 '22 16:11

schacki