简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM