DeleteRecord does not delete a record from hasMany

When I call deleteRecord() on some of my hasMany relationships, Ember Data sends a (successful) DELETE request, but the record is not deleted from the view. I show it with the render helper as follows:

 {{render "modules.list" modules}} 

Interestingly, the Ember Inspector shows that after deleteRecord() corresponding <App.Module:ember1182:null> object and its parent are null too. However, the parent element still shows the entry 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.

It seems that deleteRecord() not deleteRecord() entry from the hasMany parent array. Oddly enough, this works great in other parts of my code. One of my theories is that this is related to the {render} helper, because wherever I use it I have the same problem, but I'm not sure if this is causing the problem.

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

+5
source share
3 answers

I have found a solution. It seems that deleteRecord calls clearRelationships , which in turn sets the belongsTo model to null . However, it does not clear the opposite (i.e., it does not remove the model from the parent's hasMany relationship). The following code fixed this.

 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). This seems to be (in most cases) since it worked perfectly in other parts of my code. This situation is probably a kind of edge case. Any thoughts on what might happen are greatly appreciated.

+9
source

Setting the inverse parameter to belongsTo fix it without having to manually clear the relation in Ember Data 0.14.

 App.Item = DS.Model.Extend({ parent: belongsTo('parent', { inverse: 'items' }) }); 

So, if you create an instance of your child model through the parent as follows:

 App.ParentController = Ember.ObjectController.extend({ actions: { add: function() { this.get('items').pushObject(this.store.createRecord('item')); } } }); 

Any further call to the created instance method of the Item deleteRecord will remove it from its parent relationship and remove it from the view.

+1
source

JSBin works here, demonstrating the general idea of ​​removing items: http://jsbin.com/ucanam/1059/edit

Can you post an example of what causes the problems?

0
source

Source: https://habr.com/ru/post/970749/


All Articles