DS.Transform Class packages/ember-data/lib/transforms/base.js:1

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

1
2
3
4
5
6
7
8
9
// Converts centigrade in the JSON to fahrenheit in the app
App.TemperatureTransform = DS.Transform.extend({
  deserialize: function(serialized) {
    return (serialized *  1.8) + 32;
  },
  serialize: function(deserialized) {
    return (deserialized - 32) / 1.8;
  }
});

Usage

1
2
3
4
5
var attr = DS.attr;
App.Requirement = DS.Model.extend({
  name: attr('string'),
  temperature: attr('temperature')
});
Show:

Methods

Show:

deserialize

(serialized) Mixed

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

Example

1
2
3
deserialize: function(serialized) {
  return empty(serialized) ? null : Number(serialized);
}

Parameters:

serialized Mixed
The serialized value

Returns:

Mixed
The deserialized value

serialize

(deserialized) Mixed

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

Example

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

Parameters:

deserialized Mixed
The deserialized value

Returns:

Mixed
The serialized value