gpt4 book ai didi

javascript - 删除关联中的所有对象

转载 作者:行者123 更新时间:2023-11-28 02:36:31 26 4
gpt4 key购买 nike

给定一个看起来像这样的模型:

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 请求是什么?

最佳答案

也许使用 toArray() 方法 http://emberjs.com/api/classes/Ember.ArrayProxy.html#method_toArray应该可以工作,因为您不再直接修改 ManyArray

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

对于子项上奇怪的 PUT 请求,这是因为在删除父项时,ember-data “无效”了子项上的父属性。

关于javascript - 删除关联中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com