javascript Backbone - 将 2 个集合合并在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12384088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone - Merge 2 Collections together?
提问by gsklee
Suppose there are 2 Collections, representing respectively /api/page/1
and /api/page/2
; is there anyway (through Underscore, for example) to merge these 2 Collections into a new, single Collection?
假设有2个集合,分别代表/api/page/1
和/api/page/2
;无论如何(例如通过下划线)将这两个集合合并为一个新的单个集合?
回答by Gur Dotan
Option 1
选项1
If you haven't fetched the collection yet, you can fetch the first one, then fetch the second one with the {add : true} flag and the second one will be merged into the first one:
如果您还没有获取集合,您可以获取第一个集合,然后使用 {add : true} 标志获取第二个集合,第二个将合并到第一个中:
collection.fetch({data : {page : 2});
=> [{id: 1, article : "..."}, {id: 2, article : "..."}];
collection.fetch({data : {page : 2}, add : true });
=> [{id: 1, article : "..."}, {id: 2, article : "..."}, {id: 3, article : "..."}];
Option 2
选项 2
If you've already fetched the collections and have them stored in two variables, you can just add all contents of the second collection into the first one:
如果您已经获取了集合并将它们存储在两个变量中,则可以将第二个集合的所有内容添加到第一个集合中:
collection1.add(collection2.toJSON());
This options suffers from the fact that the first collection will fire the "add" event when the second collection is added to it. If this has side effects such as undesired UI that re-renders due to this event, you can suppress it by adding the {silent : true} flag
这个选项受到这样一个事实的影响,即第一个集合将在第二个集合添加到它时触发“添加”事件。如果这有副作用,例如由于此事件而重新呈现的不想要的 UI,您可以通过添加 {silent : true} 标志来抑制它
collection1.add(collection2.toJSON(), {silent : true});
Option 3
选项 3
If you just need the JSON format of these collections, i.e. for HTML template rendering purposes, you can just reduce everything to JS literals (arrays, objects):
如果您只需要这些集合的 JSON 格式,即用于 HTML 模板渲染目的,您可以将所有内容简化为 JS 文字(数组、对象):
collection1.toJSON().concat(collection2.toJSON());
=> [{id: 1, article : "..."}, {id: 2, article : "..."}, ...];
Option 4 = Option 2 + 3
选项 4 = 选项 2 + 3
If you've already fetched both collections, you can reduce to JS literals and add everything to a fresh Backbone collection
如果您已经获取了两个集合,则可以减少为 JS 文字并将所有内容添加到新的 Backbone 集合中
var rawCollection = collection1.toJSON().concat(collection2.toJSON());
var newCollection = new Backbone.Collection(rawCollection);
回答by Gur Dotan
try this, collection is array of models
试试这个,集合是模型数组
collection1.add(collection2.models);
if we use
如果我们使用
collection1.add(collection2.toJSON());
then reference chage
然后参考 chage
回答by Array out of bound
collection.add(models, [options])
Collection is array of models
集合是模型数组
collection.models
returns array of models
返回模型数组
Add a model (or an array of models) to the collection.
将模型(或模型数组)添加到集合中。
A = Backbone.Collection;
collection1 = new A([
{key, 'value'}, //model1
{key : value1} //model2 in collection1
]);
B = Backbone.Collection;
collection2 = new B([
{key1, 'value'}, //model1
{key1 : value1} //model2 in collection2
]);
collection1.add(collection2.models); //now, collection1 has four models..
回答by Array out of bound
use
利用
collection.models
insted of
插入的
collection.toJSON()
回答by ksol
It seems to me that by doing this, you lose the semantic tied to /api/page/{1, 2}/
.
在我看来,通过这样做,你失去了与/api/page/{1, 2}/
.
It also seems to me that neither backbone, neither underscore, provides you with a function/method doing exactly what you're trying to do.
在我看来,无论是主干还是下划线,都没有为您提供完全按照您正在尝试做的功能/方法。
So your options :
所以你的选择:
- create a new collection, add each item from your previous collections;
- add first collection items to the second one
- 创建一个新集合,添加以前集合中的每个项目;
- 将第一个收藏品添加到第二个收藏品
But you probably already knew this.
但你可能已经知道这一点。
回答by Chris A
Collection.models provides direct access to the array of moels and Collection.add will take an array of models as an argument.
Collection.models 提供对 moels 数组的直接访问,Collection.add 将接受一个模型数组作为参数。