我有模型和收藏
Model1 = Backbone.Model.extend();
Model2 = Backbone.Model.extend({
defaults: {
code:'',
m1_id: ?????, // this part should get the Model1 "id" attribute
id: '' // e.g. the value of m1.get('id');
}
});
C = Backbone.Collection.extend({
model: Model2
});
并为每个
var m1 = new Model1();
var m2 = new Model2();
var c = new C();
并设置值
m1.set({'code':'0001', 'type': 'O', id: 1});
c.add({'code':'sample1', 'id':1}); // note: i did not set the m1_id
c.add({'code':'sample2', 'id':2});
但是集合中的模型获得了 Model1 id 属性,例如
收藏品必须有这个
c.at(0).toJSON();
-> {'code':'sample1', 'm1_id': 1, id: 1} // note: the "m1_id" value is
c.at(1).toJSON(); // from Model1 "id" attribute
-> {'code':'sample2', 'm1_id': 1, id: 2}
我如何从 Model1 属性中自动设置 Collection 中的 Model2 属性。谢谢!