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
});
}
加载集合时,将发生同步/重置事件。听着,第二个集合就可以开始加载了 谢谢你的回复。有没有其他方法可以遵循?