gpt4 book ai didi

javascript - Backbone : Wait for multiple fetch to continue

转载 作者:数据小太阳 更新时间:2023-10-29 04:04:37 26 4
gpt4 key购买 nike

我获取多个页面的集合,我正在寻找一种方法来了解何时完成所有获取。这是我的收藏的样子:

app.collections.Repos = Backbone.Collection.extend({
model: app.models.Repo,
initialize: function(last_page){
this.url = ('https://api.github.com/users/' + app.current_user + '/watched');

for (var i = 1; i <= last_page; i++) {
this.fetch({add: true, data: {page: i}});
};
}, ...

知道如何使用干净的代码实现这一点吗?

最佳答案

使用 jQuery deferreds :

var deferreds = [];
for (var i = 1; i <= last_page; i++) {
deferreds.push(this.fetch({add: true, data: {page: i}}));
};

$.when.apply($, deferreds).done(function() {
...
<CODE HERE>
...
}

(我还没有实际测试过这个,但我认为它应该有效。)

关于 when 的 jQuery 文档:

Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.

另一个可能有帮助的答案:How do you work with an array of jQuery Deferreds?

关于javascript - Backbone : Wait for multiple fetch to continue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12084388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com