我正在尝试了解 Ember 数据模型,并且在尝试将使用找到的对象集合分配给ember 数据模型store.findAll()
的关系时遇到问题。hasMany
我定义了两个模型:
App.Leaf = DS.Model.extend({
text: DS.attr('string'),
branch: DS.belongsTo('App.Branch')
});
App.Branch = DS.Model.extend({
lotsOfLeaves: DS.hasMany('App.Leaf')
});
如果我将关系分配为createRecord()
App.store.loadMany(App.Leaf,[
{ id: 1, text: "Hello, I'm leaf 1", branch_id: 1 },
{ id: 2, text: "Hello, I'm leaf 2", branch_id: 1 }
]);
var allLeaves = App.store.findAll(App.Leaf);
var oneBranch = App.Branch.createRecord({ id: 1, lotsOfLeaves: allLeaves });
然后它按原样(默默地)oneBranch.get('lotsOfLeaves.length')
失败0
。
同样,如果我在关联之后分配关系,它会静默失败:
var all = App.store.findAll(App.Leaf);
oneBranch.set('lotsOfLeaves', all);
我知道我可以使用pushObject()
ononeBranch.get('lotsOfLeaves')
单独添加每片叶子,但这是唯一的方法吗?