fran
fran

Reputation: 525

Nested Routes Backbone

I'm trying to achieve nested routes with Backbonejs like this:

var Router = Backbone.Router.extend({
    routes: {
         '(/)': 'root',
         '/a/:id: 'loadA,
         '/a/:id/:secondId: 'loadA'
    }
})

Issue I am facing is that '/a/:id' is making a request to a server and I don't want to execute another request when going to /a/:id/:secondId but use the same page/view/model and change just a subview.

In addition I would like to keep track of the history, which seems to fire "initialize" when going back to the previous state.

Any idea?

Upvotes: 0

Views: 477

Answers (1)

Luke
Luke

Reputation: 8407

you have to manage the state by yourself. i do it like this

var Router = Backbone.Router.extend({
    routes: {
         '(/)': 'root',
         '/a/:id: 'loadA,
         '/a/:id/:secondId: 'loadA'
    }
})

//root
function() {
   loadRootOnce(function() {
       //done, loads only once
   })
}

//loadA
function() {
   loadRootOnce(function() {
       loadAOnce(function() {

       });
   })
}

and so on. Best practices is also to use Promises and chain them eventually

like $.when(bootstrap()).then(...)

Upvotes: 1

Related Questions