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
 
| Name | Type | Description | 
|---|---|---|
initialProperties | object | attributes and relationships that will be set  | 
store | object | the store that will define relationships  | 
options | object | supports   | 
Members
_snapshots :Array
a list of snapshots that have been taken since the record was either last persisted or since it was instantiated
- Array
 
- Default Value
 - []
 
attributeDefinitions :object
Getter find the attribute definition for the model type.
- object
 
attributeNames
Getter to just get the names of a records attributes.
attributes :object
current attributes of record
- 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')
- 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')
- 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
- 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" }
- object
 
- Default Value
 - {}
 
hasErrors :boolean
Getter to check if the record has errors.
- boolean
 
hasUnpersistedChanges :boolean
Have any changes been made since this record was last persisted?
- boolean
 
id :string
The unique document identifier. Should not change except when persisted.
- string
 
initialized :boolean
True if model attributes and relationships have been initialized
- 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
- 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
- boolean
 
- Default Value
 - false
 
isNew :boolean
True if the model has not been sent to the store
- boolean
 
persistedOrFirstSnapshot :object
the latest persisted snapshot or the first snapshot if the model was never persisted
- object
 
previousSnapshot :object
the latest snapshot
- object
 
relationshipDefinitions :object
Getter find the relationship definitions for the model 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'
- 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"
- object
 
type :string
The type of the model. Defined on the class. Defaults to the underscored version of the class name (eg 'calendar_events').
- string
 
type :string
shortcut to get the static
- string
 
Methods
_applySnapshot(snapshot)
set the current attributes and relationships to the attributes and relationships of the snapshot to be applied. also reset errors
| Name | Type | Description | 
|---|---|---|
snapshot | object | the snapshot to apply  | 
clearSnapshots()
Sets _snapshots to an empty array
destroy(options) → {Promise}
deletes a record from the store and server
| Name | Type | Description | 
|---|---|---|
options | object | params and option to skip removal from the store  | 
an empty promise with any success/error status
- Type:
 - Promise
 
errorForKey(key) → {string}
Getter to check if the record has errors.
| Name | Type | Description | 
|---|---|---|
key | string | the key to check  | 
the error text
- Type:
 - string
 
initialize(initialProperties)
Initializes observable attributes and relationships
| Name | Type | Description | 
|---|---|---|
initialProperties | object | attributes  | 
initializeAttributes(overrides)
Sets initial attribute properties
| Name | Type | Description | 
|---|---|---|
overrides | object | 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
| Name | Type | Description | 
|---|---|---|
other | object | other model object  | 
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
| Name | Type | Description | 
|---|---|---|
options | object | serialization options  | 
data in JSON::API format
- Type:
 - object
 
reload(options) → {Promise}
Replaces the record with the canonical version from the server.
| Name | Type | Description | 
|---|---|---|
options | object | props to use for the fetch  | 
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.
| Name | Type | Description | 
|---|---|---|
options | object | query params and sparse fields to use  | 
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
| Name | Type | Description | 
|---|---|---|
options | object | 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
| Name | Type | Description | 
|---|---|---|
attributes | object | 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
 
| Name | Type | Description | 
|---|---|---|
options | object | attributes and relationships to use for the validation  | 
key / value of attributes and relationship validations
- Type:
 - boolean