有没有办法在集合的开头使用create
和新模型,而不是在最后附加它?
答案 0 :(得分:8)
Backbone将插入委托给Collection.add
,这意味着您可以通过 at 选项指定位置:
var C = Backbone.Collection.extend({
url: '/echo/json/'
})
var c= new C([
{name: 1},
{name: 2},
{name: 3}
]);
c.create({name: 4}, {at: 0});
console.log(c.pluck('name'));
将产生[4,1,2,3]
答案 1 :(得分:3)
注意:现在您可以使用unshift
var ships = new Backbone.Collection;
ships.unshift([
{name: "Flying Dutchman"},
{name: "Black Pearl"}
]);