Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用主干执行不同的调用(按顺序)。收集和jQuery延迟_Javascript_Jquery_Backbone.js_Jquery Deferred - Fatal编程技术网

Javascript 使用主干执行不同的调用(按顺序)。收集和jQuery延迟

Javascript 使用主干执行不同的调用(按顺序)。收集和jQuery延迟,javascript,jquery,backbone.js,jquery-deferred,Javascript,Jquery,Backbone.js,Jquery Deferred,我使用的代码如下所示 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 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() {

});
我的目标是按顺序执行两个调用,即先执行
firstCollection
的提取,然后执行
secondCollection
的提取。其动机是第二次提取依赖于
firstCollection
属性


因此,我的问题如下。有没有更好的替代方案来实现这一点,或者jQuery延迟了,或者我使用的解决方案是正确的?

可能是这样的:

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
});
}

加载集合时,将发生同步/重置事件。听着,第二个集合就可以开始加载了

谢谢你的回复。有没有其他方法可以遵循?