2
votes

For a model which is using JSONAPISerializer as it's Serializer pushing a record for that model

into the store is done by store.push(dataInJSONAPISerializerFormat)

https://www.emberjs.com/api/ember-data/3.3/classes/DS.Store/methods/push?anchor=push

method.But for the model which is using RESTSerializer as it's Serializer pushing a record for that

model into the store is done by store.pushPayload("modelName",dataInRESTSerializerFormat)

https://www.emberjs.com/api/ember-data/3.3/classes/DS.Store/methods/pushPayload?anchor=pushPayload method.

But incase of JSONSerializer both of them are not working? Can anyone Help me?

1

1 Answers

4
votes

You misunderstood the difference between push and pushPayload methods of DS.Store.

The push method "is used to notify Ember Data's store of new or updated records that exist in the backend." It expects a "normalized JSON API document". Normalized means that it must not only be compliant with JSON API specification but also follow some even stricter conventions.

If your payload is not already a normalized JSON API document you have to convert it to some. This is normally done by a Serializer. It does not matter if this Serializer is extended of DS.JSONSerializer, DS.RestSerializer or just the base DS.Serializer class. Even if you are using a DS.JSONAPISerializer you might need to serialize your payload if it's not normalized (e.g. type is not pluralized).

The pushPayload method "is a convenience wrapper for store#push that will deserialize payloads". So this is nothing more than applying a Serializer on the provided data and pushing the normalized data into store.

A Serializer has to support store.pushPayload() by implementing a pushPayload() method. Not all Serializers shipped with Ember Data support it out of the box. DS.JSONSerializer does not. You could implement it or just call normalizeResponse() directly and passing the normalized response to store.push(). Implementing pushPayload may or may not be difficult depending on your concrete API implementation and the use cases you want to support, as you have to know the class of the primary model and the request type. Depending on your API this information may or may not be available in your payload.

Please note that there are ongoing discussions to deprecate pushPayload: https://github.com/emberjs/rfcs/issues/357. This might be another argument for not implementing a custom solution for your DS.JSONSerializer but using serializer.normalizeResponse() and store.push().