How to normalize backend responses in new Json API serializer?


#1

Hello once again everyone,

So, we have a User model which has a relationship with Employee. on the frontend the relationship is called profile, and in the past (before json api), we used attrs on serializer to match the fields.

But now, I can’t make it recognize the relationship. I tried the normalizePayload and normalize hooks on the serializer, changing payload keys, but doesn’t seem to work.

And there’s no documentation, so how should I be normalizing the backend payloads reliably?


#2

Oh, BTW, forgot to mention that I have tried using the normalizeResponse hook on the User model, but the relationships object on the payload is empty.

I assumed Ember Data would send it through the employee serializer, but it didn’t.


#3

Which serializer were/are you using? I’ve switched to the new serializer API using the REST Serializer. Had to change extractSingle -> normalizeSingleResponse and extractArray -> normalizeArrayResponse. The methods for accessing model type of changed.


#4

We were using the ActiveModelSerializer, and switched to the new JSON API serializer with new API. I’ve overwritten the normalize hook, and the model being requested get parsed and goes into store fine, but the relationships don’t. Actually I don’t even see the relationships object on the param passed

Before we’d only overwrite the modelNameFromPayloadKey and it works like a charm. Will give these hooks you mention a try later on, but do they have access to the relationships object from payload?

Not even the IDs, which is what is causing them to not be loaded at all.

Thank you for the quick response.


#5

@ToddSmithSalter, thank you. Got a baby step forward. Had to override the extractRelationships relationships, because the relationship has a dfferent name than the model name.

I guess I’ll have to come up with a solution that works globally on our projects.


#6

I found this blog post useful for how to use the new serializer hooks. It also describes what renaming you’ll need to do for existing extract hooks.


#7

Hey, guys give an example of normalizeResponse, can’t figure out how to solve simple hasMany serialization.


#8

Honestly, we (my team) gave it up for the time being. So many changes to both backend and frontend was starting to be a time sink.

We’re continuing to use Active Model Serializer. Having no proper documentation at this point, makes it really hard to do the simple things. We’ll wait for the api to settle down and docs be written. Same goes for the transition to Ember 2.0.

Don’t know for JSON API, but have you looked at the EmbeddedRecordsMixin? http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html