0

I'm using a code like the following.

var firstCollection = new FirstCollection();
$.when( firstCollection.fetch( params1 )).done(function() {

    // here firstCollection has been enriched. It contains the actual data.
    // params2 will depends on firstCollection attirbutes

    var secondCollection = new SecondCollection();
    $.when( secondCollection.fetch( params2 ) ).done(function() {

        // here secondCollection has been enriched. It contains the actual data.
        // move to another screen

    }).fail( function() {

    } );

}).fail(function() {

}).always(function() {

});

My goal is to perform two calls in sequence, i.e. perform the fetch of the firstCollection and then the fetch of secondCollection. The motivation is that second fetch depends on firstCollection attributes.

So my question is the following. Is there any better alternative to achieve this with jQuery Deferred or the solution I'm using is correct?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190

1 Answers1

0

Maybe something as this:

var firstCollection = new FirstCollection();
var secondCollection = new SecondCollection();

this.listenTo(firstCollection, 'reset', scndfetch);

firstColllection.fetch(params1).then(function(data){  })

scndfetch = function(){
secondCollection.fetch(params2).then(function(data){
    //data has model/collection
});
}

When a collection gets loaded an sync/reset event happens. Listen to that and the secondCollection can start loading..