Class DS.ActiveModelSerializer

The ActiveModelSerializer is a subclass of the RESTSerializer designed to integrate with a JSON API that uses an underscored naming convention instead of camelCasing. It has been designed to work out of the box with the active_model_serializers Ruby gem. This Serializer expects specific settings using ActiveModel::Serializers, embed :ids, embed_in_root: true which sideloads the records.

This serializer extends the DS.RESTSerializer by making consistent use of the camelization, decamelization and pluralization methods to normalize the serialized JSON into a format that is compatible with a conventional Rails backend and Ember Data.

JSON Structure

The ActiveModelSerializer expects the JSON returned from your server to follow the REST adapter conventions substituting underscored keys for camelcased ones.

Conventional Names

Attribute names in your JSON payload should be the underscored versions of the attributes in your Ember.js models.

For example, if you have a Person model:

1
2
3
4
5
App.FamousPerson = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  occupation: DS.attr('string')
});

The JSON returned should look like this:

1
2
3
4
5
6
7
8
{
  "famous_person": {
    "id": 1,
    "first_name": "Barack",
    "last_name": "Obama",
    "occupation": "President"
  }
}

Let's imagine that Occupation is just another model:

1
2
3
4
5
6
7
8
9
10
11
App.Person = DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  occupation: DS.belongsTo('occupation')
});

App.Occupation = DS.Model.extend({
  name: DS.attr('string'),
  salary: DS.attr('number'),
  people: DS.hasMany('person')
});

The JSON needed to avoid extra server calls, should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
  "people": [{
    "id": 1,
    "first_name": "Barack",
    "last_name": "Obama",
    "occupation_id": 1
  }],

  "occupations": [{
    "id": 1,
    "name": "President",
    "salary": 100000,
    "person_ids": [1]
  }]
}

Show:

Module: ember-data

The attrs object can be used to declare a simple mapping between property names on DS.Model records and payload keys in the serialized JSON object representing the record. An object with the property key can also be used to designate the attribute's key on the response payload.

Example

app/models/person.js
1
2
3
4
5
6
7
8
import DS from 'ember-data';

export default DS.Model.extend({
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  occupation: DS.attr('string'),
  admin: DS.attr('boolean')
});
app/serializers/person.js
1
2
3
4
5
6
7
8
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  attrs: {
    admin: 'is_admin',
    occupation: { key: 'career' }
  }
});

You can also remove attributes by setting the serialize key to false in your mapping object.

Example

app/serializers/person.js
1
2
3
4
5
6
7
8
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  attrs: {
    admin: {serialize: false},
    occupation: { key: 'career' }
  }
});

When serialized:

1
2
3
4
5
{
  "firstName": "Harry",
  "lastName": "Houdini",
  "career": "magician"
}

Note that the admin is now not included in the payload.

Module: ember-data

The primaryKey is used when serializing and deserializing data. Ember Data always uses the id property to store the id of the record. The external source may not always follow this convention. In these cases it is useful to override the primaryKey property to match the primaryKey of your external store.

Example

app/serializers/application.js
1
2
3
4
5
import DS from 'ember-data';

export default DS.JSONSerializer.extend({
  primaryKey: '_id'
});
Module: ember-data

The store property is the application's store that contains all records. It's injected as a service. It can be used to push records from a non flat data structure server response.