jeudi 3 septembre 2015

Ember delete records in hasMany relationship

In ember, there is a fairly straightforward way to create and persist records in a hasMany relationship:

this.get('parent').createRecord({...});          // creates a child
this.get('parent').createRecord({...});          // creates a second child

this.get('parent').save().then(function(parent){ // persists parent
    parent.get('children').save();               // persists all children
});

If we inspect our network traffic, we will see two POST requests--one for each:

PATCH localhost:3000/parent/123
POST  localhost:3000/children/
POST  localhost:3000/children/

However, there is no equivalent way to do the same for DELETE. Let's say we marked a child model for deletion by clicking a button:

deleteChild: function(child) {
    // isDeleted -> false
    child.deleteRecord();
    // isDeleted -> true
}

We can immediately say child.save(), which will fire a DELETE localhost:3000/children/1, but if I want to persist them all in bulk later as before (parent.get('children').save();), then the following will occur:

PATCH localhost:3000/parent/123
PATCH  localhost:3000/children/2

No DELETE is sent, so when I reload the page, the record comes back. The same problem occurs if I do parent.get('children').removeObject(child).

It seems to me that there should be an equivalent for the parent's createRecord like this:

this.get('parent').deleteRecord(child);

But no such functionality exists.

Note: I'm using ember 1.13, ember data 1.13, and the JSONAPIAdapter/Serializer




Aucun commentaire:

Enregistrer un commentaire