Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clearRelationship not clearing inverses. #1303

Closed
chopper opened this issue Sep 15, 2013 · 1 comment
Closed

clearRelationship not clearing inverses. #1303

chopper opened this issue Sep 15, 2013 · 1 comment

Comments

@chopper
Copy link

chopper commented Sep 15, 2013

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).

This caused unexpected behavior in my project where I successfully deleted a record and DS.ActiveModelAdapter dispatched a (successful) DELETE request to my
Rails backend - however the view still kept showing the deleted element. In the particular situation that has this issue am using the {{render}} helper to display a list of items. Other parts of my code (where I don't use {{render}}) work fine. Also see this related Stackoverflow Question.

The following code fixed the problem and allows for consistently successful deleting of child elements:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants