我创建了一个 jsFiddle,显示了将模型添加到集合时遇到的问题,然后迭代集合。发生的事情是我添加到集合中的第二个模型覆盖了我添加到集合中的第一个模型的值。
知道为什么会这样吗?
谢谢!
http://jsfiddle.net/C9wew/4734/
Event = Backbone.Model.extend({
attributes: {},
constructor: function (event) {
this.set({
eventType: event.eventType,
timestamp: event.timestamp,
sendingQuota: event.sendingQuota
});
}
});
Events = Backbone.Collection.extend({
model: Event,
initialize: function (models, options) {}
});
var collection = new Events();
var model1 = new Event({
eventType: "videoStart",
sendingQuota: 3,
timestamp: +new Date()
});
collection.add(model1);
var model2 = new Event({
eventType: "videoStart1234",
sendingQuota: 100,
timestamp: +new Date()
});
collection.add(model2);
collection.each(function (event, key, list) {
console.log("key" + key);
console.log(list[key].get("eventType"));
});