Methods
- attributes static
- constructor
- delete
- relationships static
- save
- toJSON
Properties
- adapter static
- attributes
- attributes
- idField static
- isNew
- modelName static
- serializer static
- tableName static
- type static
-
attr
Model attributes definition. This needs to be overridden to define the various attributes the model will have.
Gets passed a function attr
which should be called
multiple times (once for each attribute)
Example
attr('title', 'string')
To specify a default value, pass an options object with an appropriate default value
Example
attr('isAccepted', 'boolean', {
defaultValue: true
})
The default value can also be specified via a function
Example
attr('title', 'string', {
defaultValue: function () {
return 'default title'
}
})
Parameters:
-
attr
Function- function used to define model attributes
-
props
Parameters:
-
props
Object- properties hash that the model instance should be created with
-
relation
Model relationship definition. This should be overriden to define any relationships to other models that are needed.
Gets passed a function relation
which should be called multiple times (once for each relationship)
relation
has the following signature
relation(type, modelName, options)
type
can be either belongsTo
or hasMany
modelName
is a models Model.modelName
property
options
is an object which may contain any or all of the keys name
, keyFrom
or keyTo
Example:
relationships (relation) {
relation('hasMany', 'comment')
}
Example:
relationships (relation) {
relation('hasMany', 'comment', {name: 'comments', keyFrom: 'id', keyTo: 'postId'})
}
If not specified, name
is a pluralized version of the models name for hasMany
or singular for belongsTo
If not specified, keyFrom
is the models idField
for hasMany
or a concatenation of modelName
and the string 'Id' for belongsTo
If not specified, keyTo
is a concatenation of the related model's modelName
and the string 'Id' for for hasMany
or the related models idField
for belongsTo
Parameters:
-
relation
Function
attributes
Unknown publicProvides a copy of the models internal state as a plain javascript object Does not provide relationship data as models. For this you need to use individual relationship getters.
Example:
const model = new PostModel({
title: 'My title',
comments: [
{comment: 'This is my comment'},
{comment: 'This is another comment'}
]
})
model.comments.then(comments => {
// comments are CommentModel
instances
})
// comments are plain javascript objects in an array
const comments = model.attributes.comments
attributes
Unknown publicSetter for model instances internal state. Called in model constructor with passed in state data
Performs validation of incoming data and guards access to internal state. All individual getters and setters go through this setter thereby providing a single entry point for data validation and cleanup
Internal state is stored as a plain object and can contain nested relationship data Example: For a model
class PostModel extends Model {
static attributes (attr) {
attr('title', 'string')
}
static relationships (relation) {
relation('hasMany', 'comment')
}
}
The following is valid though unspecifiedProp
will be ignored:
new PostModel({
title: 'My title',
unspecifiedProp: true,
comments: [
{comment: 'This is my comment'},
{comment: 'This is another comment'}
]
})
The internal state of the model will be:
{
title: 'My title',
comments: [
{comment: 'This is my comment'},
{comment: 'This is another comment'}
]
}
Sub-properties:
-
props
Object- a hash of keys and values to set as the models internal data
idField
Unknown public staticSpecifies the name of the models id field.
By default the name of the models id field is 'id'
This method can be overridden to specify a different name to use for the models id field in child classes.
Example:
static get idField () {
return 'modelId'
}
By default id fields are of type 'number'.
If another type is desired then a matching named field should be provided in the model attributes hash.
Example:
static get idField () {
return 'customIdField'
}
static attributes (attr) {
attr('customIdField', 'string')
}
isNew
Unknown publicAssumes the model to be in a new state if the models id is not present in the attributes hash
modelName
Unknown public staticModel name matching filename (not classname) Used throughout Ash to reference the model
Example:
For a model class named MyPostModel
or MyPostsModel
, modelName
will be my-post