Emberデータモデルに頭を悩ませようとしていますが、使用store.findAll()
して見つかったオブジェクトのコレクションをhasMany
残り火データモデルの関係に割り当てようとして問題が発生しました。
2つのモデルを定義しています。
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()
ですか?oneBranch.get('lotsOfLeaves')