Evil Buck
Evil Buck

Reputation: 1068

How to Backbonejs nested routes or sub-routes

I want to be able to nest routes, but I can't seem to find a way to do that with backbone routers. Ideally I'd want something like:

var AppRouter = Backbone.Router.extend({
  routes: {
    "campaigns/:campaignId": "showCampaign",
    "campaigns/:campaignId/article/:articleId": "jumpToArticle"
  },

  showCampaign: function(campaignId){
    // setup Campaign state, render some content or something
  },

  jumpToArticle: function(articleId) {
    // scroll to article
  }
});

I'd expect #/campaigns/45 triggers showCampaign, but navigating from there to say #/campaigns/45/article/3 would only trigger jumpToArticle. Is there anything in Backbone or supporting library that supports this?

Upvotes: 0

Views: 241

Answers (1)

Simon Boudrias
Simon Boudrias

Reputation: 44589

Backbone.Router always trigger a single matching route. To my knowledge, only Ember.js trigger nested routes in series.

As a Backbone solution, Backbone.RouteManager would probably help you achieve something similar (with filters rather than triggered nested routes).

But if you're really looking for the same thing as Ember, then simply use their standalone router module in lieu of Backbone.Router. https://github.com/tildeio/router.js

Upvotes: 1

Related Questions