javascript 如何在特定索引中将模型插入到backbone.js 集合中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10195102/
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
How do I insert a model into a backbone.js collection in a specific index?
提问by Dine
I need to insert a model into a Collection at position Collection.length-2. The last model in the collection should always stay the last model in the collection.
我需要在 Collection.length-2 位置将模型插入到集合中。集合中的最后一个模型应始终保留为集合中的最后一个模型。
What I tried so far:
到目前为止我尝试过的:
I added one "page" model to the Collection "Pages" and then tried to swap them around by changing their sequence:
我在集合“页面”中添加了一个“页面”模型,然后尝试通过更改它们的顺序来交换它们:
var insertedpage = Pages.at(Pages.length-1);
var lastpage = Pages.at(Pages.length-2);
insertedpage.set({sequence: Pages.length-1});
lastpage.set({sequence: Pages.length});
I also tried to remove the last page, then add a new page and then add the last page back in.
我还尝试删除最后一页,然后添加一个新页面,然后重新添加最后一页。
var lastpage = Pages.pop();
Pages.add({example1: example2});
Pages.push(lastpage);
neither of these worked. The newly added page still appears as last model in the Collection. Do I need to call some kind of order function after this?
这些都没有奏效。新添加的页面仍然显示为集合中的最后一个模型。在此之后我是否需要调用某种订单函数?
采纳答案by NICCAI
Sorry for the brief answer (don't have time to respond), but look at defining a comparator function.
抱歉,回答简短(没有时间回复),但请查看定义比较器函数。
回答by Rob Hruska
Backbone.Collection.add()
takes an options
object that supports an at
key for specifying the index.
Backbone.Collection.add()
接受一个options
支持at
指定索引的键的对象。
Pass
{at: index}
to splice the model into the collection at the specifiedindex
.
通过
{at: index}
将模型拼接到指定的集合中index
。
Example:
例子:
Pages.add({ foo: bar }, { at: Pages.length - 2 })
回答by Kaleb F.
Along the same suggestion as Rob Hruska, use Backbone.Collection.add()
with at
in the options
object.
按照与 Rob Hruska 相同的建议Backbone.Collection.add()
,at
在options
对象中使用with 。
Pages = new Backbone.Collection([
{id:1, foo:'bar'},
{id:2, foo:'barista'} /* Last model should remain last */
]);
/* Insert new "page" not at the end (default) but length minus 1 */
Pages.add({id:3, foo:'bartender'}, { at: Pages.length - 1 });
Pages.at(0).id === 1; // true
Pages.at(Pages.length - 2).id === 3; // true
Pages.at(Pages.length - 1).id === 2; // true
You mentioned that Pages
seems to be sorted by the attribute sequence
; do you happen to have a comparator
function defined on the Pages collection?
你提到这Pages
似乎是按属性排序的sequence
;你碰巧comparator
在 Pages 集合上定义了一个函数吗?
Another question, did you want to update this attribute sequence
on ALL existing page models currently in the collection when a new page is added to the 2nd to the last position? Or was that attribute an attempt to accomplish your original question?
另一个问题,sequence
当新页面添加到最后一个位置时,您是否要更新当前集合中所有现有页面模型的此属性?或者该属性是否试图完成您的原始问题?