FtDRbwLXw6
FtDRbwLXw6

Reputation: 28891

Monolithic Backbone Router

I am currently learning the ins and outs of Backbone.js, but every example I can find so far uses the following router pattern or some variation of it:

var MyRouter = Backbone.Router.extend({
    routes: {
        'foo/:id': 'foo',
        'bar/:id': 'bar',
        '': 'index'
    },

    foo: function(id) { /* ... */ },

    bar: function(id) { /* ... */ },

    index: function() { /* ... */ }
});

I completely understand what's going on here, but I have a serious concern with this pattern that I (so far) haven't come across a solution to. In an application of any significant size, specifying all your routes in one place is going to create a monolithic router. Not only with respect to the routes themselves, but also the (apparent) requirement that controllers all be part of the router itself.

How do I go about organizing routes for a large-scale, modular application in Backbone.js?

P.S. If anyone could point me to a Backbone.js tutorial or example app that isn't trivially simple and chock full of bad practices, I would be grateful.

Upvotes: 2

Views: 76

Answers (2)

Dave Cadwallader
Dave Cadwallader

Reputation: 1751

I wrote a Backbone plugin that specifically addresses this problem: https://github.com/ModelN/backbone.subroute

Here's a blog post that walks through how to set things up, and some of the philosophy behind it: http://www.geekdave.com/2012/04/05/module-specific-subroutes-in-backbone/

I hope it helps!

Upvotes: 1

Alberto Zaccagni
Alberto Zaccagni

Reputation: 31560

Have a look at this example project from Derick Bailey, the creator of Backbone.Marionette.

You could see how he organized his app in modules, each of these has a router that takes care of handling the routes. The concept of modules in Marionette is similar to the one used by requirejs to load portions of code with certain dependencies.

Even if you're not going to use Marionette and just stick with Backbone you could find some helpful insights in how he divided his code. Though I suggest you to evaluate Marionette.

Upvotes: 4

Related Questions