Series: Introduction to Ember Data 2.0

Serializers-Extracting Attributes and IDs

Published on Apr 29, 2016

You can extract attributes and IDs using the normalize method, but Ember Data’s Serializers provide you with some great shortcut methods.

Learn how to use extractId, extractAttributes, and the attrs hash.


Links

Code

//serializers/quote.js
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  extractId(modelClass, resourceHash){
    return resourceHash.id.replace('quote_', '')
  },

  // the longer but more flexible way of munging attributes
  // extractAttributes(modelClass, resourceHash){
  //   resourceHash.attributes.body = resourceHash.attributes.badthink;
  //   delete resourceHash.attributes.badthink;
  //   return resourceHash.attributes;
  // },

  // the easy way to rename attributes
  attrs: {
    'body': 'badthink'
  },
})

Transcript

In the last episode, we looked at the normalize method. The normalize method is used to normalize an individual JSON object. Whereas the normalizeResponse method is used to munge the entire response all at once. And this is useful if you get say quotes coming back in the call for books or another object, because this method will normalize the quote no matter what response it’s in. Then at the end, we saw that just like normalizeResponse had the normalizeSingleResponse and normalizeArrayResponse and several helpers like that, in the same way normalize has several methods within the extract family of methods, as well as the attrs hash. These methods act as shortcuts for several common situations.

For example in the last episode, we saw at the very end that normalize can be replaced by the extractId method. And you’ll notice this line here is the same thing as this line here. It’s just a lot of this boilerplate has been removed. So you’ll see in the extractId method, it takes the same two arguments as the normalize, modelClass and resourceHash. And then it simply returns the new value of id. So whatever you were signing to id before, you just return that. And then you don’t have to call super because it already knows what to do with the results of the extractId method.

Now before we go farther in exploring other methods, I want to take a moment to say what the extraction methods are not. So there are a lot of extraction methods on- before Ember data 2.0 that have been renamed to normalizeResponse methods. And so we’re not referring to those. And there’s also some of these extract methods. The ones we’ll be referring to today are extractId, extractAttributes, extractPolymorphicRelationship, extractRelationship and extractRelationships. extractErrors and extractMeta work differently, and so are not included in this section on normalize.

You can see that from the way that they’re called. So extractId, it had modelClass and resourceHash as its arguments, just like normalize did. extractErrors on the other hand has a completely different set. And extractMeta as you’ll see, it has a similar set to extract errors which is different than extractId and the other extract methods. So extractId here, modelClass, resourceHash, same with extractAttributes and extractRelationship, although they call them something slightly different, they stick relationship before the names. It’s basically the same thing. And Polymorphic, it gives you an option but it’s pretty much the same arguments. And then if we take a look at the normalize method, we’ll see extractId, extractAttributes, and extractRelationships being called. Unfortunately as we saw last time, there is no extractType.

So let’s get to using this knowledge. So our quotes, one of the attributes has been renamed from body to badthink. And this has been predictably messing up our Ember app. So we need to make it so our Ember app accepts these new attributes. So the first way to do this is by bringing back normalize. So we’re going to reassign to the attributes and then the body attributes. So Ember data wants something in the body attribute. We’re going to give it to it. And so we’ll just basically do a rename here. And so we’ll get that from the badthink attribute. And then when we reload this, it’s working again. And of course we could be a good citizen by typing delete and deleting the attribute that we’re no longer using. That’ll clean it up for later in the pipeline.

The second way we can do this is by using extractAttributes. So we’re going to remove that part of normalize and do our extractAttributes method. Once again, it’ll take the modelClass and the resourceHash. And then we’ll be basically doing the exact same thing except we’ll just be returning the resourceHash and the attributes, because what it wants is it wants us to return the attributes hash, just like extractId wanted us to return the id. And as you can see, this isn’t saving us nearly as much code as extractId did.

There’s another way you can do it in here, where you can just create the hash on your own, and this does save a little bit if it’s a small hash. And as you can see this will also work. But for simple attribute renames like this, the best way to do it is the attrs hash. And for that we simply say okay, we have body... we know, yes, it’s going to come in as badthink, and just rename that.

So the attrs hash is the most efficient way to do attribute renames. But if you wanted to do something more complex, then extractAttributes is there for you. It’s also important to note that for extract attribute as we said before, you have to return the exact attributes hash. And that’s inconvenient if you have a lot more than just one. But with the attrs hash here, then if we have other attributes, it’ll just combine this in, so we don’t have to... let’s say we had author here. We wouldn’t have to list author as well since it’s just the same. It’s only the ones that you’re renaming.

So that’s it for today’s episode. To review, we used extractId and extractAttributes in order to change the id and attributes that were being brought in. So this is supposed to be 1 instead of quote_1. We fixed that using extractId. And this attribute was supposed to be body instead of badthink, and we also fixed that using extractAttributes. And for extractAttributes, there’s a quicker way to do it using the attrs hash. So the actual code we wrote is not very large, but knowing when to use it, that’s what’s huge.

So in the next episode in this series, we might start doing them just as pro-episodes so we can get some content for beginners in here. We’re going to go over the extractRelationship methods. If you haven’t used them before, I think you’ll be surprised at both the complexity that they cover and also the ease of use that they have for the most common situations. I’ll see you then.

Introduction to Ember Data 2.0

Subscribe to our mailing list