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

查看:92
本文介绍了deleteRecord不会从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()相应的对象是< App.Module:ember1182:null> ,其父对象为 null 也是。然而,它是父母,但仍显示其 hasMany (作为< App.Module:ember1182: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天全站免登陆