我将 ID 设置为我的模型,如下所示:
var globalCounter = 0;
var MyModel = Backbone.Model.extend({
initialize: function () {
this.set('id', globalCounter);
globalCounter += 1;
}
});
现在我将一堆实例添加MyModel
到集合中:
var i, model;
MyCollection = Backbone.Collection.extend({});
myCollection = new MyCollection();
for(i = 0; i < 3; i += 1) {
model = new MyModel();
myCollection.add(model);
}
问题是 ID 似乎没有被传递:
console.log(myCollection.pluck('id')); // gives [undefined, undefined, undefined]
为什么我添加到收藏中的模型的 ID 会消失?