Class BooleanTransform

public

The BooleanTransform class is used to serialize and deserialize boolean attributes on Ember Data record objects. This transform is used when boolean is passed as the type parameter to the attr function.

Usage

app/models/user.js
1
2
3
4
5
6
7
import Model, { attr } from '@ember-data/model';

export default class UserModel extends Model {
  @attr('boolean') isAdmin;
  @attr('string') name;
  @attr('string') email;
}

By default, the boolean transform only allows for values of true or false. You can opt into allowing null values for boolean attributes via attr('boolean', { allowNull: true })

app/models/user.js
1
2
3
4
5
6
7
import Model, { attr } from '@ember-data/model';

export default class UserModel extends Model {
  @attr('string') email;
  @attr('string') username;
  @attr('boolean', { allowNull: true }) wantsWeeklyEmail;
}

Show:

serialized
The serialized value
options
hash of options passed to `attr`
returns
The deserialized value

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

Example

1
2
3
deserialize(serialized, options) {
  return empty(serialized) ? null : Number(serialized);
}
deserialized
The deserialized value
options
hash of options passed to `attr`
returns
The serialized value

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

Example

1
2
3
4
5
import { isEmpty } from '@ember/utils';

serialize(deserialized, options) {
  return isEmpty(deserialized) ? null : Number(deserialized);
}