Class Transform

public

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

Example

app/transforms/temperature.js
import Transform from '@ember-data/serializer/transform';

// Converts centigrade in the JSON to fahrenheit in the app
export default class TemperatureTransform extends Transform {
  deserialize(serialized, options) {
    return (serialized *  1.8) + 32;
  }

  serialize(deserialized, options) {
    return (deserialized - 32) / 1.8;
  }
}

Usage

app/models/requirement.js
import Model, { attr } from '@ember-data/model';

export default class RequirementModel extends Model {
  @attr('string') name;
  @attr('temperature') temperature;
}

The options passed into the attr function when the attribute is declared on the model is also available in the transform.

app/models/post.js
import Model, { attr } from '@ember-data/model';

export default class PostModel extends Model {
  @attr('string') title;
  @attr('markdown', {
    markdown: {
      gfm: false,
      sanitize: true
    }
  })
  markdown;
}
app/transforms/markdown.js
import Transform from '@ember-data/serializer/transform';

export default class MarkdownTransform extends Transform {
  serialize(deserialized, options) {
    return deserialized.raw;
  }

  deserialize(serialized, options) {
    let markdownOptions = options.markdown || {};

    return marked(serialized, markdownOptions);
  }
}

Show:

Methods

Properties

No documented items

Events

No documented items