deleteRecord does not remove record from hasMany

前端 未结 3 1425
夕颜
夕颜 2021-01-01 03:06

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

3条回答
  •  伪装坚强ぢ
    2021-01-01 03:21

    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();
    

    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.

提交回复
热议问题