active-redux/attributes

Methods

(static) array(options)

Defines an attribute that is coerced into a array
Source:
Parameters:
Name Type Description
options Object
Name Type Description
name string JSON-API name of the attribute
default array | function Default value for this attribute
Example
class Person {
  static attributes = {
    interests: Attr.array({ default: [] }),
  };
}

const person = new Person(data);
person.interests // => ['tennis', 'squash']

const blank = new Person({});
blank.interests // => []

(static) boolean(options)

Defines an attribute that is coerced into a boolean
Source:
Parameters:
Name Type Description
options Object
Name Type Description
name string JSON-API name of the attribute
default boolean | function Default value for this attribute
Example
class Person {
  static attributes = {
    isSubscribed: Attr.boolean({ name: 'is-subscribed', default: true }),
  };
}

const person = new Person(data);
person.isSubscribed // => false

const blank = new Person({});
blank.isSubscribed // => true

(static) date(options)

Defines an attribute that is coerced into a date
Source:
Parameters:
Name Type Description
options Object
Name Type Description
name string JSON-API name of the attribute
default date | function Default value for this attribute
Example
class Person {
  static attributes = {
    birthDate: Attr.number({ default: new Date() }),
  };
}

const person = new Person(data);
person.birthDate // => Date: 1962/02/15

const blank = new Person({});
blank.birthDate // => Date: [today]

(static) hasMany(resource, options)

Defines a hasMany relationship
Source:
Parameters:
Name Type Description
resource string JSON-API type of relationship
options Object
Name Type Description
name string JSON-API name of the attribute
Example
class Person extends Model {
  static attributes = {
    posts: Attr.hasMany('articles')
  };
}

// maps to this model
class Article extends Model {
  static type = 'articles';
}

// translates to this JSON-API:
{
  relationships: {
    posts: {
      data: [
        { type: 'articles', [id] },
        { type: 'articles', [id] }
      ]
    }
  }
}

const person = new Person(data);
person.posts // => Promise<Array<Article>>

(static) hasOne(resource, options)

Defines a hasOne relationship
Source:
Parameters:
Name Type Description
resource string JSON-API type of relationship
options Object
Name Type Description
name string JSON-API name of the attribute
Example
class Person extends Model {
  static attributes = {
    employer: Attr.hasOne('companies')
  };
}

// maps to this model
class Company extends Model {
  static type = 'companies';
}

// translates to this JSON-API:
{
  relationships: {
    employer: {
      data: { type: 'companies', [id] }
    }
  }
}

const person = new Person(data);
person.employer => Promise<Company>

(static) number(options)

Defines an attribute that is coerced into a number
Source:
Parameters:
Name Type Description
options Object
Name Type Description
name string JSON-API name of the attribute
default number | function Default value for this attribute
Example
class Person {
  static attributes = {
    age: Attr.number({ default: 13 }),
  };
}

const person = new Person(data);
person.age // => 25

const blank = new Person({});
blank.age // => 13

(static) object(options)

Defines an attribute that is coerced into a object
Source:
Parameters:
Name Type Description
options Object
Name Type Description
name string JSON-API name of the attribute
default object | function Default value for this attribute
Example
class Person {
  static attributes = {
    isSubscribed: Attr.boolean({ name: 'is-subscribed', default: {} }),
  };
}

const person = new Person(data);
person.isSubscribed // => false

const blank = new Person({});
blank.isSubscribed // => true

(static) string(options)

Defines an attribute that is coerced into a string
Source:
Parameters:
Name Type Description
options Object
Name Type Description
name string JSON-API name of the attribute
default string | function Default value for this attribute
Example
class Person {
  static attributes = {
    name: Attr.string({ default: "Bob" }),
  };
}

const person = new Person(data);
person.name // => "Alexis Sanches"

const blank = new Person({});
blank.name // => "Bob"