我需要将模型插入集合中的 Collection.length-2 位置。集合中的最后一个模型应始终保留为集合中的最后一个模型。
到目前为止我尝试过的:
我向“页面”集合中添加了一个“页面”模型,然后尝试通过更改它们的顺序来交换它们:
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});
我还尝试删除最后一页,然后添加新页面,然后重新添加最后一页。
var lastpage = Pages.pop();
Pages.add({example1: example2});
Pages.push(lastpage);
这些都不起作用。新添加的页面仍显示为集合中的最后一个模型。此后我需要调用某种订单函数吗?
最佳答案
Backbone.Collection.add()
接受一个 options
对象,该对象支持用于指定索引的 at
键。
Pass
{at: index}
to splice the model into the collection at the specifiedindex
.
示例:
Pages.add({ foo: bar }, { at: Pages.length - 2 })
关于javascript - 如何将模型插入到特定索引中的backbone.js 集合中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10195102/