C class

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:

EventDescriptionArguments
beforeAddTriggered before a record is added to the associationrecord(s)_added
afterAddTriggered after a record has been added to the associationrecord(s)_added
beforeRemoveTriggered before a record is removed from the associationrecord(s)_removed
afterRemoveTriggered after a record has been removed from the associationrecord(s)_removed
beforeLoadTriggered before the association is loaded from the serverrecord(s)
afterLoadTriggered after the association has been loaded from the serverrecord(s)
*Any event can be listened forevent_name, ...arguments

Constructor

new Association(owner, reflection)
owner : Record

The parent record that owns this association

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

record : Record

The record to add to the association

Adds the record to the association and saves that record to the server

Parameters

record : Record

The record to add to the association

options : Object optional

Options to pass to the request

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 optional

Event options

once : boolean optional

Whether the event should only trigger once

context : object optional

The context to bind the event to

listener : object optional

The 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 optional

Event options

once : boolean optional

Whether the event should only trigger once

context : object optional

The 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

attributes : Object | null

Attributes to initialize the new record with

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

record : Record

The record to remove from the association

Removes the record from the association and sends delete for that record to the server

Parameters

record : Record

The record to remove from the association

options : Object optional

Options to pass to the request

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 optional

Event name(s) or object of event/callback pairs

callback : function | object | null optional

Callback function or options object if name is an object

options : object optional

Event options

once : boolean optional

Match only events set to trigger once

context : object optional

Match only events bound to this context

listener : object optional

Match 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 optional

The object to stop listening to; if omitted, stops listening to all objects

name : string | Array.<string> | object optional

Event name(s) or object of event/callback pairs

callback : function optional

Callback function to remove

options : object optional

Event options

once : boolean optional

Match only events set to trigger once

context : object optional

Match only events bound to this context

Returns

EventBus

Returns this for chaining

Sends a request to the server for the associated resource

Parameters

record : Record

The record to send the request for

options : Object

Options to pass to the request

Returns

Promise

A promise that resolves with the server response

Sets attributes on the target record of this association

Parameters

attributes : Object

Attributes to set on the target

dirty : boolean

Whether to mark the association as dirty

Classes