我有以下方法对我的集合执行提取,然后调用 render
函数来显示结果。这一切都工作正常,并且每次用户到达页面底部时都会触发它。
但是,在这次获取过程中,我想预获取下一批项目,以便为用户提供更好的体验。我不知道如何实现这一目标。我想过再做一次 this.collection.fetch
(检查我的代码),然后将结果分配给一个变量,但我不确定这是否是不好的做法。
那么我也许可以查阅这个变量,看看下次用户到达页面底部时它是否有任何内容?
有人做过类似的事情并可以给我一些指导吗?
loadItems: function() {
var that = this;
this.collection.fetch({
data: {"offset": this.offset, "limit": this.limit},
success: function () {
that.render();
that.offset += that.limit;
//Maybe I can do another fetch here
this.collection.fetch({
data: {"skip": this.skip, "limit": this.limit, "sort": this.sortKey}
success: function () {
//However I'm not really sure how I would get the results and assign them to a variable
}
});
}
});
},
render: function() {
var self = this;
_(this.collection.models).each(function(product){
self.appendItem(product);
}, this);
},
appendItem: function(product) {
var productView = new ProductView({
model: product
});
$(this.el).find(".products-list").append(productView.render().el);
}
最佳答案
为什么不在加载主页时获取?
var data = collection.fetch();
当它触底时,你 view.render(update);然后通过再次获取来更新数据 data = collection.fetch();
所以数据保持收集状态,当它第二次触底时,它将显示已经获取并存储在数据中的内容,然后再次获取并更新下一次触底的数据...
var data;
collection.fetch({
success: function(model) {
data=model.toJSON();
}
})
关于javascript - 如何使用 Backbone 预取集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929606/