Model

The base class for data records

Constructor

new Model(initialProperties, store, options)

  • Sets the store and id.
  • Sets jsonapi reference to relationships as a hash.
  • Makes the predefined getters, setters and attributes observable
  • Initializes relationships and sets attributes
  • Takes a snapshot of the initial state
Parameters:
NameTypeDescription
initialPropertiesobject

attributes and relationships that will be set

storeobject

the store that will define relationships

optionsobject

supports skipInitialization

Members

_snapshots :Array

a list of snapshots that have been taken since the record was either last persisted or since it was instantiated

Type:
  • Array
Default Value
  • []

attributeDefinitions :object

Getter find the attribute definition for the model type.

Type:
  • object

attributeNames

Getter to just get the names of a records attributes.

attributes :object

current attributes of record

Type:
  • object

defaultAttributes

getter method to get the default attributes

dirtyAttributes :Set

A list of any attribute paths which have been changed since the previous snapshot

const todo = new Todo({ title: 'Buy Milk' }) todo.dirtyAttributes => Set() todo.title = 'Buy Cheese' todo.dirtyAttributes => Set('title') todo.options = { variety: 'Cheddar' } todo.dirtyAttributes => Set('title', 'options.variety')

Type:
  • Set

dirtyRelationships :Set

A list of any relationship paths which have been changed since the previous snapshot We check changes to both ids and types in case there are polymorphic relationships

const todo = new Todo({ title: 'Buy Milk' }) todo.dirtyRelationships => Set() todo.note = note1 todo.dirtyRelationships => Set('note')

Type:
  • Set

endpoint :string

The canonical path to the resource on the server. Defined on the class. Defaults to the underscored version of the class name

Type:
  • string

errors :object

A hash of errors from the server

todo = store.find('todos', 5)
todo.errors
=> { authorization: "You do not have access to this resource" }
Type:
  • object
Default Value
  • {}

hasErrors :boolean

Getter to check if the record has errors.

Type:
  • boolean

hasUnpersistedChanges :boolean

Have any changes been made since this record was last persisted?

Type:
  • boolean

id :string

The unique document identifier. Should not change except when persisted.

Type:
  • string

initialized :boolean

True if model attributes and relationships have been initialized

Type:
  • boolean

isDirty :boolean

True if the instance has been modified from its persisted state

NOTE that isDirty does NOT track changes to the related objects but it does track changes to the relationships themselves.

For example, adding or removing a related object will mark this record as dirty, but changing a related object's properties will not mark this record as dirty.

The caller is reponsible for asking related objects about their own dirty state.

todo = store.add('todos', { name: 'A good thing to measure' })
todo.isDirty
=> true
todo.name
=> "A good thing to measure"
await todo.save()
todo.isDirty
=> false
todo.name = "Another good thing to measure"
todo.isDirty
=> true
await todo.save()
todo.isDirty
=> false
Type:
  • boolean

isInFlight :boolean

True if the instance is coming from / going to the server

todo = store.find('todos', 5)
// fetch started
todo.isInFlight
=> true
// fetch finished
todo.isInFlight
=> false
Type:
  • boolean
Default Value
  • false

isNew :boolean

True if the model has not been sent to the store

Type:
  • boolean

persistedOrFirstSnapshot :object

the latest persisted snapshot or the first snapshot if the model was never persisted

Type:
  • object

previousSnapshot :object

the latest snapshot

Type:
  • object

relationshipDefinitions :object

Getter find the relationship definitions for the model type.

Type:
  • object

relationshipNames

Getter to just get the names of a records relationships.

relationships :object

The reference to relationships. Is observed and used to provide references to the objects themselves

todo.relationships => { tag: { data: { type: 'tags', id: '1' } } } todo.tag => Tag with id: '1'

Type:
  • object

snapshot :object

The current state of defined attributes and relationships of the instance Really just an alias for attributes

todo = store.find('todos', 5)
todo.title
=> "Buy the eggs"
snapshot = todo.snapshot
todo.title = "Buy the eggs and bacon"
snapshot.title
=> "Buy the eggs and bacon"
Type:
  • object

type :string

The type of the model. Defined on the class. Defaults to the underscored version of the class name (eg 'calendar_events').

Type:
  • string

type :string

shortcut to get the static

Type:
  • string

Methods

_applySnapshot(snapshot)

set the current attributes and relationships to the attributes and relationships of the snapshot to be applied. also reset errors

Parameters:
NameTypeDescription
snapshotobject

the snapshot to apply

clearSnapshots()

Sets _snapshots to an empty array

destroy(options) → {Promise}

deletes a record from the store and server

Parameters:
NameTypeDescription
optionsobject

params and option to skip removal from the store

Returns:

an empty promise with any success/error status

Type: 
Promise

errorForKey(key) → {string}

Getter to check if the record has errors.

Parameters:
NameTypeDescription
keystring

the key to check

Returns:

the error text

Type: 
string

initialize(initialProperties)

Initializes observable attributes and relationships

Parameters:
NameTypeDescription
initialPropertiesobject

attributes

initializeAttributes(overrides)

Sets initial attribute properties

Parameters:
NameTypeDescription
overridesobject

data that will be set over defaults

initializeRelationships()

Initializes relationships based on the relationships hash.

isSame(other) → {boolean}

Comparison by identity returns true if this object has the same type and id as the "other" object, ignores differences in attrs and relationships

Parameters:
NameTypeDescription
otherobject

other model object

Returns:

if this object has the same type and id

Type: 
boolean

jsonapi(options) → {object}

getter method to get data in api compliance format TODO: Figure out how to handle unpersisted ids

Parameters:
NameTypeDescription
optionsobject

serialization options

Returns:

data in JSON::API format

Type: 
object

reload(options) → {Promise}

Replaces the record with the canonical version from the server.

Parameters:
NameTypeDescription
optionsobject

props to use for the fetch

Returns:

the refreshed record

Type: 
Promise

rollback()

restores data to its last persisted state or the oldest snapshot state if the model was never persisted

todo = store.find('todos', 5)
todo.name
=> "A good thing to measure"
todo.name = "Another good thing to measure"
todo.rollback()
todo.name
=> "A good thing to measure"

save(options) → {Promise}

creates or updates a record.

Parameters:
NameTypeDescription
optionsobject

query params and sparse fields to use

Returns:

the persisted record

Type: 
Promise

takeSnapshot(options)

take a snapshot of the current model state. if persisted, clear the stack and push this snapshot to the top if not persisted, push this snapshot to the top of the stack

Parameters:
NameTypeDescription
optionsobject

options to use to set the persisted state

undo()

restores data to its last state state if the model was never persisted

updateAttributes(attributes)

Updates attributes of this record via a key / value hash

Parameters:
NameTypeDescription
attributesobject

the attributes to update

validate(options) → {boolean}

Checks all validations, adding errors where necessary and returning false if any are not valid Default is to check all validations, but they can be selectively run via options:

  • attributes - an array of names of attributes to validate
  • relationships - an array of names of relationships to validate
Parameters:
NameTypeDescription
optionsobject

attributes and relationships to use for the validation

Returns:

key / value of attributes and relationship validations

Type: 
boolean