Documentation / @ember-data/model / index / attr
Function: attr()
Call Signature
function attr(): DataDecorator;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:163
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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
function attr<T>(type): DataDecorator;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:164
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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
function attr(type): DataDecorator;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:165
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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
function attr(options): DataDecorator;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:166
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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
function attr<T>(type, options?): DataDecorator;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:167
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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
function attr(type, options?): DataDecorator;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:168
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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 | PrimitiveValue | unknown[]> & object
a hash of options
Returns
DataDecorator
Call Signature
function attr(
target,
key,
desc?): void;Defined in: warp-drive-packages/legacy/declarations/model/-private/attr.d.ts:169
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. WarpDrive 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
import { Model, attr } from '@warp-drive/legacy/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.
import { Model, attr } from '@warp-drive/legacy/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:
import { Model, attr } from '@warp-drive/legacy/model';
export default class PostModel extends Model {
@attr('text', {
uppercase: true
})
text;
}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