DS.JSONSerializer#attrs

attrs{Object}

Defined in addon/serializers/json.js:111

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
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
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
import DS from 'ember-data';

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

When serialized:

{
  "firstName": "Harry",
  "lastName": "Houdini",
  "career": "magician"
}

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

doc_EmberJs
2016-11-30 16:49:47
Comments
Leave a Comment

Please login to continue.