O'Reilly logo

Mastering Ember.js by Mitchel Kelonye

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating custom transformations

Transformations are the different types of model attributes. Application authors are not limited to the built-in ones, so they may define their own transformations easily. For example, one of our backend services may represent Booleans as zeros and ones:

1 – true
0 – false

We could create a transformation that resolves these values where necessary:

App.BinaryBoolean = DS.Transform.extend({
  serialize: function(boolean){
    return (!boolean)
     ? 0
     : 1;
  },
  deserialize: function(binary){
    return (!!!binary)
     ? false
     : true;
  }
});

We just created a new transformation by extending DS.Transform and then defining the following two methods that act on the attribute's value:

  • serialize: This converts the attribute value to a form acceptable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required