Association
Base class for all Viking record associations.
Associations provide a way to define relationships between models. This class serves as the foundation for specific association types like BelongsTo, HasMany, HasOne, and HasAndBelongsToMany.
Events
All associations emit the following events that can be used to react to changes:
| Event | Description | Arguments |
|---|---|---|
| beforeAdd | Triggered before a record is added to the association | record(s)_added |
| afterAdd | Triggered after a record has been added to the association | record(s)_added |
| beforeRemove | Triggered before a record is removed from the association | record(s)_removed |
| afterRemove | Triggered after a record has been removed from the association | record(s)_removed |
| beforeLoad | Triggered before the association is loaded from the server | record(s) |
| afterLoad | Triggered after the association has been loaded from the server | record(s) |
| * | Any event can be listened for | event_name, ...arguments |
Constructor
new Association(owner, reflection)
reflection
:
Record.Reflection
The reflection that defines this association
Instance Properties
Object containing references to objects this instance is listening to
Whether the association has been changed since last loaded/saved
Object containing all event listeners
Whether this association contains a collection of records
Unique identifier for this event bus instance
Whether the association has been loaded from the server
The owner/parent record that this association belongs to
The reflection that defines this association
The associated record(s) - can be a single model or array of models depending on association type
Instance Methods
Adds a record to the association
Parameters
Adds the record to the association and saves that record to the server
Parameters
Returns
Promise
A promise that resolves when the record has been added
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
Creates a deep clone of this association
Returns
Association
A new association instance with cloned targets
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
Instantiates a new record as the target of this association
Parameters
Determines if this association needs to be saved
Returns
boolean
True if the association or its target has unsaved changes
Reloads the association from the server
Returns
Promise
A promise that resolves with the reloaded association
Removes a record from the association
Parameters
Removes the record from the association and sends delete for that record to the server
Parameters
Returns
Promise
A promise that resolves when the record has been removed
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
Sends a request to the server for the associated resource
Parameters
Returns
Promise
A promise that resolves with the server response
Sets attributes on the target record of this association
Parameters
dirty
:
boolean
Whether to mark the association as dirty
