2012-11-15 38 views
1

,看起來像這樣所有對象:刪除在給定一個模型的關聯

App.Parent = Ember.Model.extend(
    children: DS.attr.hasMany('App.Child') 
) 

App.Child = Ember.Model.extend(
    parent: DS.attr.belongsTo('App.Parent') 
) 

parent = App.Parent.find(1) 

# How do I remove parent and all of it's children? 
# This doesn't work since I'm removing elements from an array while iterating it 
parent.get('children').forEach(c -> c.deleteRecord()) 
parent.deleteRecord() 

# Only removing the parent like this won't work either, 
# Ember-data generates some strange PUT requests for every child 
parent.deleteRecord() 

# I guess I could do this, but it feels really awkward and 
# wrong to use the adapter directly. 
# And it also side-steps transactions making bulk updates impossible 
App.store.adapter.deleteRecords(App.store, App.Child, parent.get('children')) 
parent.deleteRecord() 
App.store.commit() 

有沒有更直接的方式是什麼,當僅刪除父生成了奇怪的PUT請求?

回答

0

普通的JavaScript,而不是CoffeeScript的,但我認爲你將能夠轉換:

var children = parent.get('children'), 
    i = children.length; 

while (i--) { 
    children[i].deleteRecord(); 
} 
1

也許使用的toArray()方法http://emberjs.com/api/classes/Ember.ArrayProxy.html#method_toArray應該工作,因爲你沒有更多的直接修改ManyArray

parent.get('children').toArray().forEach(c -> c.deleteRecord()) 

對於孩子們的奇怪PUT請求,這是因爲當刪除父母時,ember-data「廢除」子孩的父母屬性。

相關問題