我遇到了将两个 Backbone.js 收藏合并到一起的问题...
我做了很多搜索,并提出了一篇与此问题非常类似的文章:Backbone - Merge 2 Collections together?
这个现有的答案提供了很多方法,但它们都不适合我。
以下是来自 Backbone.js 类的代码片段,其中包含此问题。我试着用它来评论我能够更好地解释我的问题。
var THAT=this;
//create dynamic channel collection
this.data.dynamicChannelsCollection = new DynamicChannelsCollection();
this.data.dynamicChannelsModel = new DynamicChannelsModel();
this.data.sortModel = new TVPModel({
sortField:'date_created',
sortDirection:'DESC',
pageItems:10,
page:0,
searchString:'',
searchFields:['text']
});
this.data.dynamicChannelsCollection.sortedFetch(this.data.sortModel);
// merge the dynamic channel collection into the standard channel collection
console.log('dynamicChannelsCollection: ', this.data.dynamicChannelsCollection);
console.log('collectionListCollection: ', THAT.data.collectionListCollection);
// HOW DO I MERGE THE 2 COLLECTIONS ABOVE PROPERLY?
// What I've Tried:
// 0) THAT.data.collectionListCollection.toJSON().concat(this.data.dynamicChannelsCollection.toJSON());
// 1) THAT.data.collectionListCollection.add(this.data.dynamicChannelsCollection.toJSON());
// 2) THAT.data.collectionListCollection.add(this.data.dynamicChannelsCollection.toJSON(), {silent:true});
// 3) THAT.data.collectionListCollection.add(this.data.dynamicChannelsCollection.toJSON());
// 4) THAT.data.collectionListCollection.add(this.data.dynamicChannelsCollection.models);
// 5) THAT.data.collectionListCollection.add(this.data.dynamicChannelsCollection.toJSON(), { silent:true });
// 6) THAT.data.collectionListCollection.add(this.data.dynamicChannelsCollection.models, { silent:true });
// 7)
//var mergeCollection = this.data.dynamicChannelsCollection.toJSON().concat(THAT.data.collectionListCollection.toJSON());
//THAT.data.collectionListCollection = new Backbone.Collection(mergeCollection);
// I need THAT.data.collectionListCollection to be a merged array of the two arrays above
console.log('collections merged: ', THAT.data.collectionListCollection);
有两个console.logs
输出两个收藏集以验证它们是否存在。
然后有第三个日志输出合并的集合。
以下两个截图是我每次收到的结果......
合并的集合总是包含0或4个元素(当它应该包含7个元素时)。
如何正确合并这两个集合?
以下是来自console.log的DynamicChannelCollection的内容:
child {app: Object, length: 0, models: Array[0], _byId: Object, url: "/api/entity?n=10&o=date_created&od=DESC&entityType=5"}
_byId
:
Object
app
:
Object
length
:
3
models
:
Array[3]
0
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c77"
collection
:
child
id
:
"66431935"
__proto__
:
constructor
1
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c78"
collection
:
child
id
:
"66431934"
__proto__
:
constructor
2
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c79"
collection
:
child
id
:
"66431933"
__proto__
:
constructor
length
:
3
__proto__
:
Array[0]
url
:
"/api/entity?n=10&o=date_created&od=DESC&entityType=5"
__proto__
:
constructor
以下是来自console.log的CollectionListCollection的内容:
child {app: Object, length: 0, models: Array[0], _byId: Object}
_byId
:
Object
_events
:
Object
app
:
Object
hasFetched
:
true
length
:
4
models
:
Array[4]
0
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c80"
collection
:
child
id
:
"66431932"
__proto__
:
constructor
1
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c81"
collection
:
child
id
:
"66431931"
__proto__
:
constructor
2
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c82"
collection
:
child
id
:
"66431930"
__proto__
:
constructor
3
:
child
_changing
:
false
_events
:
Object
_pending
:
false
_previousAttributes
:
Object
app
:
Object
attributes
:
Object
changed
:
Object
cid
:
"c83"
collection
:
child
id
:
"66252499"
__proto__
:
constructor
length
:
4
__proto__
:
Array[0]
url
:
"/api/player?n=10&o=date_created&od=DESC"
__proto__
:
constructor
我根据请求在合并之前添加了Collections
的文本。每个Models
中的Collection
肯定是彼此不同的。