0

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?

fran
  • 515
  • 2
  • 11
  • 22

1 Answers1

1

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(...)

Luke
  • 8,235
  • 3
  • 22
  • 36
  • Thanks, but the resource is loaded by loadA using the ID and then I'm supposed to reuse that resource for '/a/:id/:secondId' – fran May 02 '16 at 15:41
  • 1
    yeah, thats what i said with `and so on`. because you can nest as many calls as you want with this nested-fetch-once pattern. all those functions save internally the results, so they actually fetch only once (and trigger a change) and only those who are not loaded yet, will load. but every one gives back the results you need. – Luke May 02 '16 at 20:29