How to get removed models for delete their from backend?


#1

Hello.

I have model which has hasMany relationship - points. In template I render objects from this relationship. And when I want to remove one point from routePoints I do it like this:

removePoint(point) {
  this.get('model.points').removeObject(point);

  if (point.get('isNew')) {
    point.rollbackAttributes();
  } else {
    point.deleteRecord();
  }
},

And then if user will save model I will delete removed points from backend:

// Inside save action
model.hasMany('points').hasManyRelationship.canonicalState.forEach(function(point) {
  if (point.isDeleted()) {
    point.save();
  }
});

model.save();

I’m not sure I’m doing it right. How can I get deleted model from store another way? Is there a more correct practice?


#2

you can use filterBy to get the deleted points (and use invoke to shorten the function even more):

store.get('points').filterBy('isDeleted').invoke('save')

#3

Thank you for answer, but it won’t be work.I remove point from model.points.


#4

Then the question is not clear for me. In your last sentence you asked to get deleted models from store. But you can also do:

model.get('points').filterBy(...)

#5

No I can’t, becouse I remove point from points

this.get('model.points').removeObject(point);

#6

Now I understand. Sorry for my wrong answer and unfortunately I don’t know another solution. In one of my apps I’m solving this issue on the backend.


#7

you should instead invoke deleteRecord(), followed by save().

please refer

https://guides.emberjs.com/v2.11.0/models/creating-updating-and-deleting-records/#toc_deleting-records

https://guides.emberjs.com/v2.11.0/models/relationships/#toc_removing-relationships


#8

Nope. I must remove all deleted points only after user will click “Save” button. In save action I save main model and all new/edited/deleted points.


#9
  1. In removePoint() method, You might want to use a temporary array for holding ‘deleted’ records.
  2. while saving parent record, iterate over the array and invoke destroyRecord() on each of them.