我有两套收藏品.一个用于类别,另一个用于项目.我等待类别完成为我提取所有内容以设置要获取的项目的类别.
此外,每次我点击一个类别我必须重新获取一个新的项目集合,因为我每次点击一个类别,它不刷新或重新获取集合,所以分页代码搞乱了错误的集合.有任何想法吗?
this.categoryCollection = new CategoryCollection();
this.categoryCollection.fetch();
this.itemCollection = new ItemCollection();
this.itemCollection.fetch();
Run Code Online (Sandbox Code Playgroud)
san*_*oIT 56
刚遇到类似的情况.我最终将jquery.ajax参数传递给fetch()调用.您可以使第一次获取同步.从骨干文档:
jQuery.ajax选项也可以直接作为fetch选项传递
您的代码可以简化为:
this.categoryCollection.fetch({async:false});
this.itemCollection.fetch();
Run Code Online (Sandbox Code Playgroud)
Rob*_*ska 15
一种快捷的方法是将回调传递给fetch()调用第二个调用的第一个调用.fetch()获取options支持success(和error)回调的对象.
var self = this;
this.categoryCollection = new CategoryCollection();
this.categoryCollection.fetch({
success: function () {
self.itemCollection = new ItemCollection();
self.itemCollection.fetch();
}
});
Run Code Online (Sandbox Code Playgroud)
不是最优雅,但它的工作原理.您可以使用延迟来执行一些创造性的东西,因为fetch()返回由$.ajax发生的调用创建的jQuery延迟.
对于分页问题,如果没有看到您的分页代码正在做什么就很难分辨.你将不得不自己滚动分页内容,因为Backbone本身不支持它.我可能要做的是为正在查询的页面条件创建一个新的Collection,并且可能创建一个我可以命中的服务器操作,它将支持分页(将Collection映射url到分页服务器操作).不过,我并没有对此深思熟虑.