Class: Model

.base. Model

A base Model that other models inherit from. It also has a few static methods that are useful in creating subclasses.

new Model()

Extends

Members


json

Returns a JSON representation of this model, as a string

Methods


<static> defineArray(object, propName, model [, initialValue])

Adds an array property to the provided object with the given property name. The array property will be enumerable but non-configurable and non-writable. Each item added to the array will be made an instance of the provided model.
Parameters:
Name Type Argument Default Description
object Object The object to add the array property to
propName String The name of the array property to add
model function A constructor function to apply to each item before being added to the array.
initialValue Array <optional>
[] The intial set of items to add to the array property.

<static> defineEnumProp(object, propName, values, initialValue)

Adds a property to the provided object which may only have the specified values
Parameters:
Name Type Description
object Object The object to add the property to
propName String The name of the property to add
values Array An array of acceptable values for the new property
initialValue Any The initial value to give to the new property. Will be `undefined` if none is provided.
Returns:
Returns the original object with the new property added
Type
Object

<static> defineProp(object, propName, model [, initialValue])

Adds a property with the given name to the provided object, and ensures that the value of the property is always an instance of the provided model. The property will be enumerable and writable but non-configurable.
Parameters:
Name Type Argument Description
object Object The object to add the property to
propName String The name of the property to add
model function A constructor function. The value of this property will always be an instance of the provided function.
initialValue Any <optional>
The initial value to use for the value of this property. If none is provided, the value will be undefined until the property is set.
Returns:
Returns the original object with the new property added
Type
Object

<static> simplify(model [, required])

Takes an Object and an Array of required properties, and returns a simplified version of the object with all empty Arrays, Strings, Sets, Maps, and basic Objects removed. This is useful when trying to reduce the footprint of the object when stringifying it.
Parameters:
Name Type Argument Default Description
model Object The Object to simplify
required Array <optional>
[] A Array of required property names. These properties will not be removed from the object, even if they are empty
Returns:
Returns a *new* object with the properties removed
Type
Object

destroy()

A no-op destroy function. This method should be overwritten on the subclass with logic to delete the model from the database.

emit(event [, args])

Triggers the callbacks for a given event. Subsequent arguments will be passed to the event callbacks.
Parameters:
Name Type Argument Description
event String The event name to trigger
args Any <optional>
<repeatable>
Additional arguments to pass to the event callback
Inherited From:
Returns:
Returns a Promise that resolves when all callbacks are complete
Type
Object

off( [event] [, callback])

Unsubscribe from one or more events on this object
Parameters:
Name Type Argument Description
event String <optional>
The event to remove. If none is specified, all events will be removed.
callback function <optional>
The callback to remove. If none is specified, all callbacks for the specified event will be removed.
Inherited From:
Returns:
Returns the Emitter object
Type
Object

on(event, callback)

Subscribe to an event on this object, by binding a callback function to the name of the event. Callbacks may be Promises or regular functions.
Parameters:
Name Type Description
event String | Object The name of the event to subscribe to. May also be an object mapping event names to callback functions
callback function The callback function to invoke whenever the specified event is fired
Inherited From:
Returns:
Returns the emitter object
Type
Object

once(event, callback)

Subscribe to an event on this object for one time only. After the event is triggered, the callback will be removed. Callbacks may be Promises or regular functions.
Parameters:
Name Type Description
event String The name of the event to subscribe to
callback function The callback function to invoke when the event is fired
Inherited From:
Returns:
Returns the emitter object
Type
Object

save()

A no-op save function. This method should be overwritten on the subclass with logic to save the model data to a database.