I'm working on an Ember app that is using Ember Data and the now default json-api adapter.
According to the json-api spec (http://jsonapi.org/format/#crud-deleting) when deleting a record your server should return a 200
response if the deletion is successful and the server responds with just a top level meta
key.
My current server does just this and I'm trying to figure out how to access the data in the top level meta object when using Ember Data's model.destroyRecord()
method.
myModel.destroyRecord().then(function(model){
// the returned value is the model. How can I get the actual metadata
// returned by the server?
});
The server response contains information about what exactly was deleted and looks like this:
{
"meta": {
num-deleted-a: 10,
num-deleted-b: 100,
num-deleted-c: 200
}
}
I'd like to get this information so I can display it to the user.
Thank you!
I am using the following versions:
Ember : 2.2.0
Ember Data : 2.3.3
jQuery : 1.11.3
Ember does not support
meta
for single model requests (find
,save
anddestroyRecord
) at the moment!If you want this you have to hook into ember internals.
The following code uses ember internals from ember 2.3 and may break in future versions!
There is the undocumented
_metadataFor
function on thestore
that gives you the last metadata for a given type. I use a custom initializer to always save it to theModel
:After this you can do
model.save().then(() => console.log(model.get('meta')))
ormodel.destroyRecord.then(() => console.log(model.get('meta')))
.Maybe checkout this ember-twiddle.
After upgrading to Ember 2.6.1 and Ember 2.6.1 I was no longer able to access the
store._metadataFor
property.To get access to the metadata from a particular call I now override the serializer for the model and add a
meta
property to the model itself that simply passes through the metadata.As an example I have a record type called
vote
which when saved returns some metadata.For the model I do the following:
Then in the serializer for the vote model I do the following:
Note that in the above example I'm only adding in metadata to the vote model when making a save call to the store. If you wanted to always add in the metadata then you would override the
normalize
method instead of thenormalizeSaveResponse
method.Then you can access a
meta
field in the results of your save call.