deleteRecord 不会从 hasMany 中删除记录 [英] deleteRecord does not remove record from hasMany

查看:17
本文介绍了deleteRecord 不会从 hasMany 中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对某些 hasMany 关系调用 deleteRecord() 时,Ember Data 发送(成功)DELETE 请求,但该记录并未从视图中删除.我使用 render 助手显示它,如下所示:

When I call deleteRecord() on some of my hasMany relations, Ember Data sends a (successful) DELETE request, but the record is not removed from the view. I am displaying it using the render helper like this:

{{render "modules.list" modules}}

有趣的是,Ember Inspector 显示在 deleteRecord() 之后对应的对象是 而它的父对象是 null,也是.然而,当我重新加载页面然后调用 deleteRecord(),它按预期从视图中删除.

The interesting thing is that Ember Inspector reveals that after deleteRecord() the corresponding object is <App.Module:ember1182:null> and its parent is null, too. It's parent, however, still shows the record in its hasMany (as <App.Module:ember1182:null>) When I reload the page and then call deleteRecord(), it is removed from the view as expected.

似乎 deleteRecord() 没有从父级的 hasMany 数组中删除记录.奇怪的是,这在我代码的其他部分运行良好.我的一个理论是,这与 {render} 助手有关,因为无论我在哪里使用它,我都会遇到同样的问题,但我不确定这是否是导致问题的原因.

It seems that deleteRecord() does not remove the record from the parent's hasMany array. Oddly enough this works fine in other parts of my code. One theory I have is that this has to do with the {render} helper, because wherever I use that I have the same issue, but I am not sure if that's what's causing the problem.

我正在使用最新版本的 ember 数据 (commit 2511cb1f77).

I am using the latest build of ember data (commit 2511cb1f77).

推荐答案

我找到了解决方案.看起来deleteRecord 调用了clearRelationships,它反过来将模型的belongsTo 设置为null.但是它并没有清除逆(即它不会从父级的 hasMany 关系中删除模型).以下代码修复了它.

I found the solution. It seems like deleteRecord calls clearRelationships, which in turn sets the belongsTo of the model to null. However it does not clear the inverse (i.e. it does not remove the model from the hasMany relation of the parent). The following code fixed it.

var model = this.get('model');
model.eachRelationship(function(name, relationship){
    if (relationship.kind === "belongsTo") {
        var inverse = relationship.parentType.inverseFor(name);
        var parent  = model.get(name);
        if (inverse && parent) parent.get(inverse.name).removeObject(model);
    }
});
this.get('model').deleteRecord();
this.get('model').save();

但是我认为这应该由 Ember (Data) 处理.似乎是(大部分时间),因为这在我的代码的其他部分运行良好.所以这种情况很可能是某种边缘情况.任何关于可能发生的事情的想法都非常感谢.

However in my opinion this should be handled by Ember (Data). It seems like it is (most of the time), since this worked fine in other parts of my code. So likely this situation is some sort of edge case. Any thoughts on what might be going on are much appreciated.

这篇关于deleteRecord 不会从 hasMany 中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆