Series: Introduction to Ember Data 2.0

Serializers-normalize${specific}Response

Published on Apr 22, 2016

Customizing normalizeResponse directly works, but is a bit of a blunt instrument. Learn how Ember Data Serializer’s specific response normalization methods (like normalizeArrayResponse) can improve your code (and save your sanity).


Links

Code

import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  normalizeArrayResponse(store, primaryModelClass, payload, id, requestType){
    payload.data.forEach((book)=>{
      book.type = 'book';
    })
    return this._super(...arguments)
  },
  normalizeSingleResponse(store, primaryModelClass, payload, id, requestType){
    payload.data.type = 'book'
    return this._super(...arguments)
  }
});

Transcript

Hey and welcome back to our series on serializers. In the last episode, we showed our data where the type accidentally got changed from book to kindling, and then we created a normalizeResponse method, we overwrote the normalizeResponse method in order to fix that. However as we said at the end, normalizeResponse is the Swiss Army chainsaw of Ember data serializer methods. And so in the interest of making our code readable, we’re going to use more specific normalizeResponse methods before this if statement gets out of hand.

So there are a lot of specific normalizeResponse methods, and I’m calling it a specific normalizeResponse method if it’s normalize something else Response, in this case normalizeArrayResponse.

So it may be helpful to look at what is in the default normalizeResponse method. So as you can see, it’s just a big switch statement that it looks at the requestType, and then it bumps it to one of these normalize specific response methods, depending on what the requestType is. Then if we dig a little deeper into these normalize specific responses, we’ll see that they’re all calling either normalizeSingleResponse, normalizeArrayResponse, or normalizeSaveResponse. And then normalizeSaveResponse ends up calling normalizeSingleResponse as well. So it’s all being filtered through either normalizeSingleResponse or normalizeArrayResponse. And that happens to match the split that we have here for arrays up here and single, just one object, right here.

So let’s go ahead and rewrite this using those two methods. So we’ll comment this out, and then we’ll first get a normalizeArrayResponse, and we’ll also have a normalizeSingleResponse. And so for the normalizeArrayResponse we’ll do this. We’ll have our payload.data, and forEach, we’ll just loop through those, and then for normalizeSingleResponse, we’ll take this code right here and call it and then we’ll call super. And then when we run our app... we have to return these. Always make sure to return super, not just call it. And it is working great.

So you can see how it’s a little bit more clear what each of these pieces of code are doing. So here we had to check like oh, is it... it’s an array, okay, you have to look at that line, and you could do something like let isArray = blah blah blah, but these are very much standard, and so you can recognize them at a glance. They also have the advantage for if you have... for example, most of your single responses are the same, but you have one where you are say finding the BelongsToResponse, and that needs to be a little bit different than your normal SingleResponse. So you can just overwrite this, do what you need to do, and call super without having to do here... you would have to do a nested if statement in here, and those can get really messy.

You may haven’t noticed this before, but the sole reason that we have all of these methods are just so that you can have a place to put in what you need, because findAllResponse and FindHasManyResponse, those are not doing different things by default. But if you want them to do different things, they can.

So that covers all thirteen normalize specific response methods. They’re all called in basically this fashion. You have your signature which is always the same, then you do your action and you call return this._super. So I hope this helps you when you’re dealing with your JSON responses. One method that is not covered by this normalize specific response is the just plain normalize method. That method is different from what we’ve covered this episode because it doesn’t cover the entire response. It just covers one little JSON object, and then it’s applied to all JSON objects of that type. So join us next week as we discuss that. I’ll see you then.

Introduction to Ember Data 2.0

Subscribe to our mailing list