Class DS.Transform

The DS.Transform class is used to serialize and deserialize model attributes when they are saved or loaded from an adapter. Subclassing DS.Transform is useful for creating custom attributes. All subclasses of DS.Transform must implement a serialize and a deserialize method.

Example

app/transforms/temperature.js
import DS from 'ember-data';

// Converts centigrade in the JSON to fahrenheit in the app
export default DS.Transform.extend({
  deserialize: function(serialized) {
    return (serialized *  1.8) + 32;
  },
  serialize: function(deserialized) {
    return (deserialized - 32) / 1.8;
  }
});

Usage

app/models/requirement.js
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  temperature: DS.attr('temperature')
});

Show:

Module: ember-data
serialized

The serialized value

returns

The deserialized value

When given a serialize value from a JSON object this method must return the deserialized value for the record attribute.

Example

deserialize: function(serialized) {
  return empty(serialized) ? null : Number(serialized);
}
Module: ember-data
deserialized

The deserialized value

returns

The serialized value

When given a deserialized value from a record attribute this method must return the serialized value.

Example

serialize: function(deserialized) {
  return Ember.isEmpty(deserialized) ? null : Number(deserialized);
}