Model
An in-memory data object with typed, change-tracked attributes.
Model provides the attribute layer shared by all data objects: schema-driven getters/setters, type coercion, default values, and dirty-state tracking. It is suitable for memory-level objects that are never queried or persisted.
{@link Record} extends Model to add querying, persistence, and associations.
It includes support for:
- Type coercion through attribute schema definitions
- Change tracking and dirty checking
- Single-table inheritance naming (modelName/baseClass)
- Event system for attribute changes
Constructor
new Model()
Instance Properties
Object containing references to objects this instance is listening to
Object containing all event listeners
Unique identifier for this event bus instance
Instance Methods
Bind an event to a callback function. Passing "*" will bind
the callback to all events fired.
If the options.once is true, the event will only be triggered a single time. After the first time the callback is invoked, its listener will be removed. If multiple events are passed in as an array, the handler will fire once for each event, not once for a combination of all events.
Parameters
name
:
string
|
Array.<string>
|
object
Event name(s) or object of event/callback pairs
callback
:
function
|
object
Callback function or options object if name is an object
options
:
object
optionalEvent options
once
:
boolean
optionalWhether the event should only trigger once
context
:
object
optionalThe context to bind the event to
listener
:
object
optionalThe listener object
Returns
EventBus
Returns this for chaining
Tell this object to listen to an event in another object, while keeping track of what it's listening to for easier unbinding later.
Parameters
obj
:
object
|
NodeList
The object to listen to events on
name
:
string
|
Array.<string>
|
object
Event name(s) or object of event/callback pairs
callback
:
function
|
object
Callback function or options object if name is an object
options
:
object
optionalEvent options
once
:
boolean
optionalWhether the event should only trigger once
context
:
object
optionalThe context to bind the event to
Returns
EventBus
Returns this for chaining
Returns names of all attributes that have changed
Returns
Array.<string>
Array of attribute names that have changed
Returns all changes made to this record since it was last persisted
Returns
Object
Object containing changed attributes with [oldValue, newValue] pairs
Creates a clone of this record
Returns
Model
A new instance with the same attributes and change state
Hook for subclasses to declare and set up instance properties they need in
place before attributes are applied (e.g. {@link Record} sets up its
persistence flags and attaches associations here, since setAttributes may
reference them). Called after the attribute accessors are defined and before
setDefaultAttributes/setAttributes. Blank by default.
Parameters
Dispatches the change event for a single attribute, then notifies each collection tracking this model.
Parameters
key
:
string
The attribute that changed
oldValue
:
*
The previous value of the attribute
newValue
:
*
The new value of the attribute
Dispatches the record-level change event for a batch of attribute changes, then notifies each collection tracking this model.
Parameters
Trigger one or many events, firing all bound callbacks. Callbacks are
passed the same arguments as dispatchEvent is, apart from the event name
(unless you're listening on "*", which will cause your callback to
receive the true name of the event as the first argument).
Parameters
name
:
string
|
Array.<string>
Event name or array of event names
args
:
*
Arguments to pass to the event handlers
Returns
EventBus
Returns this for chaining
Clears the tracked changes, marking the current attributes as the clean baseline. This is the in-memory analog of persisting — {@link Record#persist} builds on it — without any notion of being saved to a server.
Returns
Model
This instance for chaining
Returns true if the given attribute is in the attributes hash, otherwise false
Parameters
attributeName
:
string
The name of the attribute to check
Returns
boolean
True if the attribute exists
Checks if the record or a specific attribute has changed
Parameters
attributeName
:
string
optionalOptional attribute name to check
Returns
boolean
True if the record or specified attribute has changed
Hook for the consuming stack to run custom setup after a record is fully
constructed and its attributes applied. Blank by default; override without
calling super.
Parameters
Get the value of an attribute
Parameters
attributeName
:
string
The name of the attribute to read
Returns
*
The attribute value
Remove one or many callbacks that match the arguments. If called with no arguments, removes all callbacks for all events.
Parameters
name
:
string
|
Array.<string>
|
object
|
null
optionalEvent name(s) or object of event/callback pairs
callback
:
function
|
object
|
null
optionalCallback function or options object if name is an object
options
:
object
optionalEvent options
once
:
boolean
optionalMatch only events set to trigger once
context
:
object
optionalMatch only events bound to this context
listener
:
object
optionalMatch only events bound by this listener
Returns
EventBus
Returns this for chaining
Tell this object to stop listening to either specific events or to every object it's currently listening to.
Parameters
obj
:
object
|
NodeList
optionalThe object to stop listening to; if omitted, stops listening to all objects
name
:
string
|
Array.<string>
|
object
optionalEvent name(s) or object of event/callback pairs
callback
:
function
optionalCallback function to remove
options
:
object
optionalEvent options
once
:
boolean
optionalMatch only events set to trigger once
context
:
object
optionalMatch only events bound to this context
Returns
EventBus
Returns this for chaining
Sets a single attribute value
Parameters
attribute
:
string
The attribute name to set
value
:
*
The value to set
Returns
Model
This record instance for chaining
Sets multiple attribute values at once
Parameters
coerced
:
boolean
optionalWhether the attributes have already been coerced
Returns
Model
This record instance for chaining
Returns an object containing the default values for this record type based on the schema definition
Returns
Object
Default values for this record
Returns a copy of the record's attributes for JSON serialization, so
JSON.stringify(record) emits the attributes rather than internal
state. Attribute values serialize themselves (e.g. Date via
Date#toJSON).
Returns
Object
A copy of the record's attributes
Static Methods
Gets the base class in the inheritance hierarchy for this model
Returns
Class.<Model>
The base class of this model
Coerces attribute values according to their type definitions in the schema
Parameters
Returns
Object
The coerced attributes
Gets the inheritance attribute for this model
Returns
string
|
boolean
The inheritance attribute or default 'type'
Events
Fired when a specific attribute on a record is changed
Parameters
oldValue
:
*
The previous value of the attribute
newValue
:
*
The new value of the attribute
