Series: Introduction to Ember Data 2.0

Serializers-keyForAttribute and keyForRelationship

Published on May 13, 2016

The keyFor methods are great for dealing with app-wide patterns in how your attribute and relationship keys are formatted.



import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
  keyForAttribute(key, method){
    return `${this._super(...arguments)}_attribute`

  keyForRelationship(key, relationship, method){
    return this._super(...arguments).toUpperCase();


Welcome back to our series on Ember Data 2.0 Serializers. So in this episode, we’re going to go over the keyFor methods. Now we’ve gone over a lot of methods which help pull out stuff individually, and I’ll elaborate more by what I mean on that, but the keyFor methods are for doing larger trends, things in your API that affect everything, and fixing it all in one place. Let’s get concrete with that.

In this example, we have an attribute that from the server we have it labeled body_attribute, but on the client we just think of it as body. So we already know a way to get rid of that. So what we can do is have an attrs hash. It’ll have the client key and then the server key. So that’s pretty easy to do. But what happens when we have more attributes like this?

So in our books we’ve created these attributes and we’ve changed them and added a _attribute to the end of each of them. So as you can see, this messes up some stuff. It’s not getting these attributes correctly. But we could go in and in our book we could add an attrs hash which fixes all of these. And with this, we’ll be able to get all the data that we need. So you can see how this could get pretty hairy if we had a large library. Fortunately, keyForAttribute is here to help us.

So the first thing we’re going to do is we’re going to generate a new serializer, and we’re just going to call it base. So it’s the default serializer right now. And what we’re going to do is before we make changes to it, we’re going to inherit from it so that quote and book will get whatever is defined in here. And they’ll still be JSONAPISerializers, since that’s what base is based on. So we’re going to delete these attrs and then do it all in one fell swoop in the base serializer.

So keyForAttribute is the method that we’re going to use, and it takes two parameters. The first parameter is the key and the second is the method. method is either serialize or deserialize, and we’ll talk about that a little bit at the end. So the key is the key on the Ember side, and so in one of our attrs if we go back to that, it would be the one on the left, and what we’ll be returning is the one on the right, the key for the server side, so whatever it starts off with and then attribute in this case.

So that’s a fairly easy one. And let’s go ahead and see if it works. So it works mostly. We’ll see that publish year is still undefined. That’s because here publish-year has a dash, and dash was already being taken care of by the default keyForAttribute and it was turning it CamelCase. So what we need to do is instead of directly passing in the key, we need to call super and pass in the results of that. And with that we’ll see that the publish year is correct.

So that’s one of the keyFor methods. We won’t go over keyForLink because we haven’t been using links. But we will go over keyForRelationship briefly. So I’ve basically turned everything uppercase. So here in quotes BOOK is uppercase, and here in books QUOTES is uppercase. And once again, we could take care of this with an attrs hash. So this attrs hash, it’s translating from the lowercase to the uppercase, and things are working, and we could do the same for quote, but then we have to do that for every relationship. And while this relationship is currently... there aren’t that many, the app will grow, and we don’t want to have to add one of these every time. So we’ll delete these and we use keyForRelationship. So this is going to take three parameters, key, relationship, and method. And once again, we’ll be returning what is coming from the server. So we’ll be taking the key from the Ember app and returning what’s in the server. So in this case, we’ll start with super since we already know that’s important, and then we’ll just call toUpperCase, which should put everything in all-capitals, like it is on the server. And that makes everything work again, and working for both quotes and books.

So that’s how you use both of these methods, keyForAttribute and keyForRelationship. There is one more thing. I told you I’d tell you about this method parameter that both of them get. This method parameter, what that is is it’s either serialize or deserialize. And what we’ve been doing in all the videos up until now have been deserialization, getting stuff from the server and putting it on to the Ember app. What serialization is is taking stuff from the Ember app and putting it up to the server. We give that method in keyFor methods so that you can decide if it takes something different, like if it sends something down but it expects something different up, you can handle that. That should be pretty rare. I can’t think of any good reasons to do it, but that doesn’t mean it’s not out there.

So speaking of serialization, that’s what we’re going to be covering next. We’re going to be covering both the serialize method and the specific serialize methods that follow on from that. I’ll see you then.

Introduction to Ember Data 2.0

Subscribe to our mailing list