Backbone.js 获取主干。集合由其他集合组成
因此,我有几种类型的数据:Backbone.js 获取主干。集合由其他集合组成,backbone.js,collections,Backbone.js,Collections,因此,我有几种类型的数据: Post 项目 事件 每个数据模型都有自己的集合和查看路径: /posts => app.postsCollection /projects => app.projectsCollection /events => app.eventsCollection 现在我想添加另一条路线: /=>app.everythingCollection 如何创建一个集合,该集合显示其他三个集合的集合,但不再次获取所有后期项目和事件数据 类似地,调用eve
Post
项目
事件
每个数据模型都有自己的集合和查看路径:
/posts => app.postsCollection
/projects => app.projectsCollection
/events => app.eventsCollection
现在我想添加另一条路线:
/=>app.everythingCollection
如何创建一个集合,该集合显示其他三个集合的集合,但不再次获取所有后期项目和事件数据
类似地,调用everythingCollection.fetch()
将填充postsCollection
、projectsCollection
和eventsCollection
,以便在单独呈现数据时,数据可用
关键是永远不要下载相同的数据两次
var EverythingCollection = Backbone.Model.extend({
customFetch: function (){
var collections = [app.postsCollection, app.projectsCollection, app.eventsCollection],
index = -1,
collection,
that = this;
this.reset(); //clear everything collection.
//this function check collections one by one whether they have data or not. If collection don't have any data, go and fetch it.
function checkCollection() {
if (index >= collections.length) { //at this point all collections have data.
fillEverything();
return;
}
index = index + 1;
collection = collections[index];
if (collection && collection.models.length === 0) { //if no data in collection.
collection.fetch({success: function () {
checkCollection();
}});
} else { //if collection have data already, go to next collection.
checkCollection();
}
}
function fillEverything() {
collections.forEach(function (collection) {
if (collection) {
that.add(collection.models); //refer this http://backbonejs.org/#Collection-add
}
});
}
}
});
像下面这样使用
app.everythingCollection = new EverythingCollection();
app.everythingCollection.customFetch();
if (app.postsCollection.models.length === 0) {
app.postsCollection.fetch();
}
对于其他集合,请在获取数据之前检查models
length。像下面这样
app.everythingCollection = new EverythingCollection();
app.everythingCollection.customFetch();
if (app.postsCollection.models.length === 0) {
app.postsCollection.fetch();
}
像下面这样使用
app.everythingCollection = new EverythingCollection();
app.everythingCollection.customFetch();
if (app.postsCollection.models.length === 0) {
app.postsCollection.fetch();
}
对于其他集合,请在获取数据之前检查models
length。像下面这样
app.everythingCollection = new EverythingCollection();
app.everythingCollection.customFetch();
if (app.postsCollection.models.length === 0) {
app.postsCollection.fetch();
}
在应用程序启动时,将所有必需的集合存储在一个数组或对象中,将事件侦听器连接到每个侦听第一个重置事件的集合,并记住在第二个数组中获取的集合。如果使用了需要所有集合的路由,则可以为已获取的集合获取阵列中未找到的集合:
fetch(options){
this.fetchAll();
return Backbone.Collection.prototype.fetch.call(this, options);
}
(未经测试,但它会让你知道我该怎么做)
在everythingCollection中执行此操作,您就拥有了所需的everythingCollection.fetchAll()功能。您还可以重写everythingCollection的fetch函数,以首先获取所有其他集合:
fetch(options){
this.fetchAll();
return Backbone.Collection.prototype.fetch.call(this, options);
}
在应用程序启动时,将所有必需的集合存储在一个数组或对象中,将事件侦听器连接到每个侦听第一个重置事件的集合,并记住在第二个数组中获取的集合。如果使用了需要所有集合的路由,则可以为已获取的集合获取阵列中未找到的集合:
fetch(options){
this.fetchAll();
return Backbone.Collection.prototype.fetch.call(this, options);
}
(未经测试,但它会让你知道我该怎么做)
在everythingCollection中执行此操作,您就拥有了所需的everythingCollection.fetchAll()功能。您还可以重写everythingCollection的fetch函数,以首先获取所有其他集合:
fetch(options){
this.fetchAll();
return Backbone.Collection.prototype.fetch.call(this, options);
}
你的
应用程序。everythingCollection
不必是真正的主干收藏。它只需要访问和获取其他收藏即可
您可以继承主干.Events以获得所有事件设施
var fetchedRecords = {posts: 0, projects: 0, events: 0};
var Everything = function () {}
_.extend(Everything.prototype, Backbone.Events, {
fetch: function (option) {
that = this;
this.count = 0;
option.success = function () {that.doneFetch(arguments)};
if (fetchRecords.posts == 0) {
option.fetchedName = "posts";
app.postsCollection.fetch(option);
this.count ++;
}
if (fetchRecords.projects == 0) {
option.fetchedName = "projects";
app.projectsCollection.fetch(option);
this.count ++;
}
if (fetchRecords.events == 0) {
option.fetchedName = "events";
app.eventsCollection.fetch(option);
this.count ++;
}
},
donefetch: function (collection, response, options) {
if (this.count <=0) return;
this.count --;
if (this.count == 0) {
if (options.reset) this.trigger("reset");
}
fetchedRecords[options.fetchedName] ++;
},
posts: function () {return app.postsCollection},
projects: function () {return app.projectsCollection},
events: function () {return app.eventsCollection}
});
app.everythingCollection = new Everything;
everythingView.listenOn app.everythingCollection, "reset", everythingView.render;
app.everythingCollection.fetch({reset: true});
var fetchedRecords={posts:0,projects:0,events:0};
var Everything=function(){}
_.扩展(一切、原型、主干、事件、{
获取:函数(选项){
那=这个;
此值为0.count;
option.success=function(){that.doneFetch(arguments)};
if(fetchRecords.posts==0){
option.fetchedName=“posts”;
app.postsCollection.fetch(选项);
这个是.count++;
}
if(fetchRecords.projects==0){
option.fetchedName=“项目”;
app.projectscolection.fetch(选项);
这个是.count++;
}
if(fetchRecords.events==0){
option.fetchedName=“事件”;
app.eventsCollection.fetch(选项);
这个是.count++;
}
},
donefetch:函数(收集、响应、选项){
如果(this.count您的应用程序。everythingCollection
不一定是真正的主干集合。它所需要的只是访问和获取其他集合
您可以继承主干.Events
以获得所有事件设施
var fetchedRecords = {posts: 0, projects: 0, events: 0};
var Everything = function () {}
_.extend(Everything.prototype, Backbone.Events, {
fetch: function (option) {
that = this;
this.count = 0;
option.success = function () {that.doneFetch(arguments)};
if (fetchRecords.posts == 0) {
option.fetchedName = "posts";
app.postsCollection.fetch(option);
this.count ++;
}
if (fetchRecords.projects == 0) {
option.fetchedName = "projects";
app.projectsCollection.fetch(option);
this.count ++;
}
if (fetchRecords.events == 0) {
option.fetchedName = "events";
app.eventsCollection.fetch(option);
this.count ++;
}
},
donefetch: function (collection, response, options) {
if (this.count <=0) return;
this.count --;
if (this.count == 0) {
if (options.reset) this.trigger("reset");
}
fetchedRecords[options.fetchedName] ++;
},
posts: function () {return app.postsCollection},
projects: function () {return app.projectsCollection},
events: function () {return app.eventsCollection}
});
app.everythingCollection = new Everything;
everythingView.listenOn app.everythingCollection, "reset", everythingView.render;
app.everythingCollection.fetch({reset: true});
var fetchedRecords={posts:0,projects:0,events:0};
var Everything=function(){}
_.扩展(一切、原型、主干、事件、{
获取:函数(选项){
那=这个;
此值为0.count;
option.success=function(){that.doneFetch(arguments)};
if(fetchRecords.posts==0){
option.fetchedName=“posts”;
app.postsCollection.fetch(选项);
这个是.count++;
}
if(fetchRecords.projects==0){
option.fetchedName=“项目”;
app.projectscolection.fetch(选项);
这个是.count++;
}
if(fetchRecords.events==0){
option.fetchedName=“事件”;
app.eventsCollection.fetch(选项);
这个是.count++;
}
},
donefetch:函数(收集、响应、选项){
如果(this.count听起来像或可能解决您的问题
下一步构建Soundcloud也值得一试(请参阅视图间共享模型)。它们在解决此问题方面与上述两个插件有类似的方法。这听起来像是或可能会解决您的问题
下一步构建Soundcloud也值得一试(请参见视图间共享模型)。他们在解决这个问题时采用了与上述两个插件类似的方法。对于如此简单的问题,似乎是一个非常老套的答案。这不是老套的方法。你这样认为吗?:)如果你需要引入另一个集合,比如app.commentsCollection
,你可以简单地将其添加到collections
数组中。它就是这么简单。对于如此简单的事情,这似乎是一个非常老套的答案。这不是老套的方式。你这样认为吗?:)如果您需要引入另一个集合,如app.commentsCollection
,您只需将其添加到collections
数组中即可。就这么简单。此线程可能会有帮助:。此线程可能会有帮助:。如果他执行app.postsCollection.fetch()操作,是否只提取尚未提取的集合然后运行app.everythingCollection.fetch(),希望只获取项目和事件?我已经编辑了我的答案。和其他人一样,你需要在某个地方保留一个获取记录。每次当你选择