Skip to content

Documentation / @ember-data/model / index / attr

Call Signature

ts
function attr(): DataDecorator;

Defined in: packages/model/src/-private/attr.ts:269

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Returns

DataDecorator

Call Signature

ts
function attr<T>(type): DataDecorator;

Defined in: packages/model/src/-private/attr.ts:270

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Type Parameters

T

T

Parameters

type

TypeFromInstance<T>

the attribute type

Returns

DataDecorator

Call Signature

ts
function attr(type): DataDecorator;

Defined in: packages/model/src/-private/attr.ts:271

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters

type

string

the attribute type

Returns

DataDecorator

Call Signature

ts
function attr(options): DataDecorator;

Defined in: packages/model/src/-private/attr.ts:272

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters

options

AttrOptions

a hash of options

Returns

DataDecorator

Call Signature

ts
function attr<T>(type, options?): DataDecorator;

Defined in: packages/model/src/-private/attr.ts:273

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Type Parameters

T

T

Parameters

type

TypeFromInstance<T>

the attribute type

options?

OptionsFromInstance<T>

a hash of options

Returns

DataDecorator

Call Signature

ts
function attr(type, options?): DataDecorator;

Defined in: packages/model/src/-private/attr.ts:274

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters

type

string

the attribute type

options?

AttrOptions< | object | unknown[] | PrimitiveValue> & object

a hash of options

Returns

DataDecorator

Call Signature

ts
function attr(
   target, 
   key, 
   desc?): void;

Defined in: packages/model/src/-private/attr.ts:275

attr defines an attribute on a Model. By default, attributes are passed through as-is, however you can specify an optional type to have the value automatically transformed. EmberData ships with four basic transform types: string, number, boolean and date. You can define your own transforms by subclassing Transform.

Note that you cannot use attr to define an attribute of id.

attr takes an optional hash as a second parameter, currently supported options are:

  • defaultValue: Pass a string or a function to be called to set the attribute to a default value if and only if the key is absent from the payload response.

Example

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;
  @attr('boolean', { defaultValue: false }) verified;
}

Default value can also be a function. This is useful it you want to return a new object for each attribute.

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

export default class UserModel extends Model {
  @attr('string') username;
  @attr('string') email;

  @attr({
    defaultValue() {
      return {};
    }
  })
  settings;
}

The options hash is passed as second argument to a transforms' serialize and deserialize method. This allows to configure a transformation and adapt the corresponding value, based on the config:

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

export default class PostModel extends Model {
  @attr('text', {
    uppercase: true
  })
  text;
}
app/transforms/text.js
js
export default class TextTransform {
  serialize(value, options) {
    if (options.uppercase) {
      return value.toUpperCase();
    }

    return value;
  }

  deserialize(value) {
    return value;
  }

  static create() {
    return new this();
  }
}

Parameters

target

object

key

string | symbol

desc?

PropertyDescriptor

Returns

void

Released under the MIT License.