Search code examples
ember.jsember-datajson-api

Ember JsonApi Serialize override normalizeResponse. How to interpret the parameters


I use the JsonApiSerializer in Ember.js and I want to override standard behavior. I want to behavior to be different for certain models. Therefore I would like check the parameter primaryModelClass to determine the model type, but how do I interpret that parameter? I have tried to inspect it but all I get is "unknown mixin".

I have looked at the documentation here:

https://emberjs.com/api/ember-data/3.0/classes/DS.JSONAPISerializer/methods/normalizeResponse?anchor=normalizeResponse

I have this piece of code

import DS from 'ember-data';
export default DS.JSONAPISerializer.extend({
    normalizeResponse: function(store, 
                                primaryModelClass, 
                                payload, id, requestType) {
    console.log(primaryModelClass.toString());
...

It outputs (unknown mixin)

How do I fint the model type in the normalizeResponse method?

I use Ember 3.0.


Solution

  • So the documentation isn't super clear on this, but you're pretty close:

    You need to use primaryModelClass.modelName which will return a string of the model name.

    https://www.emberjs.com/api/ember-data/3.0/classes/DS.JSONAPISerializer/methods/normalizeResponse?anchor=normalizeResponse

    However, this may not be the method you want. There are more specific methods for the type of operation you need: normalize<storeMethod>Response: ie. normalizeQueryResponse.

    If you need specific transformations for specific models, you can generate model-specific serializers as well. So you can create a serializer for your post model which is different than your comment model by running ember generate serializer post