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?
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')
Thank you for answer, but it won’t be work.I remove point from model.points.
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(...)
No I can’t, becouse I remove point from points
this.get('model.points').removeObject(point);
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.
you should instead invoke deleteRecord(), followed by save().
please refer
Creating Records
You can create records by calling the createRecord() method on the store.
store.createRecord('post', { title: 'Rails is Omakase', body: 'Lorem ipsum' });
The store object is available in controllers and routes using...
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.