Javascript 删除关联中的所有对象
给定一个类似于以下内容的模型:Javascript 删除关联中的所有对象,javascript,coffeescript,ember.js,ember-data,Javascript,Coffeescript,Ember.js,Ember Data,给定一个类似于以下内容的模型: 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 childr
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请求是什么?纯JavaScript而不是CoffeeScript,但我认为您可以转换:
var children = parent.get('children'),
i = children.length;
while (i--) {
children[i].deleteRecord();
}
纯JavaScript而不是CoffeeScript,但我认为您可以转换:
var children = parent.get('children'),
i = children.length;
while (i--) {
children[i].deleteRecord();
}
也许使用toArray()方法应该有效,因为您不再直接修改ManyArray
parent.get('children').toArray().forEach( c -> c.deleteRecord() )
对于子对象上奇怪的PUT请求,这是因为在删除父对象时,余烬数据会“取消”子对象上的父对象属性。也许使用toArray()方法应该有效,因为您不再直接修改ManyArray
parent.get('children').toArray().forEach( c -> c.deleteRecord() )
对于子对象上奇怪的PUT请求,这是因为在删除父对象时,余烬数据会使子对象上的父属性“无效”