Transforms

Ember Data gives you the ability to transform data from your API to fit the needs of your application. You have already seen in Chapter 21 that Ember Data has built-in transforms – the methods DS.attr('string'), DS.attr('boolean'), DS.attr('number'), and DS.attr('date').

When you add a transform to your application, you can call DS.attr with your target attribute type. Transforms are like JavaScript coercion – they take a value and return the value in a specified type.

Here is an example of a basic DS.attr('object') transform:

export default DS.Transform.extend({ deserialize(value) { if (!Ember.$.isPlainObject(value)) { return {}; } else { return value; } }, serialize(value) { if (!Ember.$.isPlainObject(value)) { ...

Get Front-End Web Development: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.