Saving models and their relationships using the new Ember Data JSONAPISerializer/Adapter

Hi.

I have implemented a JSON-API backend and I’m using ember (1.13.4) and ember data (1.13.5) to pull my data in to the store using the new JSONAPIAdapter / JSONAPISerializer. All is working well and the store is populated with all of the models and their attributes/relationships.

However, when I call the .save() method on my models the JSON-API-formatted request that is generated doesn’t contain the “included” key with all related models inside it. This means that if I modify an attribute in a related model and then save the primary model I don’t see the changed attribute from the related model in the JSON payload.

What is the correct way to save models and their relationships so they are embedded using the new JSONAPIAdapter / JSONAPISerializer? I tried the EmbeddedRecordsMixin but this resulted in null attribute values within the “attributes” key of the payload.

If anyone can assist me with this I’d be grateful.

Thanks.

3 Likes

Can you try this solution: json - Ember Data: Saving relationships - Stack Overflow

Thanks for your response but as I said in my post I have tried the EmbeddedRecordsMixin mentioned in your suggested solution but it resulted in null attributes being assigned to the parent record in the JSON-API “attributes” key. This may just be a bug and perhaps the EmbeddedRecordsMixin is the right way to go but I don’t know.

Can anyone can shed any light on whether this is the right approach or not?

There is another answer (accepted one) below: json - Ember Data: Saving relationships - Stack Overflow

1 Like

@danr1979 This does not answer your question, but in the past I tried to use EmbeddedRecordsMixin and ran into a lot of similar issues around adapter/serializer usage. I ended up using Ember Model Fragments instead. I’ve personally found it to be very stable, functional and well maintained.

Thank you to both of you.

The fragments idea is interesting & looks very useful but I’m not going to use it as I want to stick with json-api requests/responses.

The other answer on stack overflow is similar to a solution I used when serializing a model to display in a non-ember library that needed json input. The thing is, I wouldn’t have expected to have to create a custom serializer to serialize my models in a default json-api format when I am using the JSONAPIserializer.

Maybe someone from the core team might be able to clear this one up?

Does anyone know how to get the “included” attribute to be present in the POST when saving a model with relationships as per my description above please? Do I have to manually do it by adding related records to each relationship or does the JSON-API serializer do it out of the box?

Any help would be appreciated.

Thanks.

The standard does not support this at the moment and is scheduled for v1.1 (http://discuss.jsonapi.org/t/json-api-weekly-meeting-june-22nd-2015/23).

Your best bet would be to inherit from the serializer and implement your own standard for now. Just remember to include the included paths in your URL so that your server can reply with all the updated entities.

Thanks for this update. I’ll look in to modifying the serializer for now. I see here that the issue is being worked on. Does anyone contributing to the json-api effort have any idea when version 1.1 is scheduled for a release?

Hi,

There is already a solution for this question? I’m using JSONAPIAdapter and JSONAPISerializer too. Me too had the same problem with the relationships keys.

Thanks!