Model

Model

new Model(data, (options))

A Model is really just sugar over a class(object).

Why use it over jut a plain object or class instance? A Metamon Model provides methods for setting default data, defining a schema to validate field properties, setting views (whitelisted or blacklisted) properties on your data.

Source:
Parameters:
Name Type Description
data Object
(options) Object

[{ defaults: { id: {String}, modified: {Number} created: {Number} }, schema: { : { type: } }, - an enjoi JSON schema https://github.com/tlivings/enjoi views: { : { whitelist: Array, // only supports one or the other for each view blacklist: Array } } }]

Extends

Methods

addView(name, fields, (isWhitelist))

adds a view, which is a POJO slice of the data model.

Views are a way of creating a white or black lists of fields referable by a unique string name. This allows Model instances to have various representations while still adhering to DRY.

Source:
Parameters:
Name Type Description
name String

unique name for a view

fields Array.<String>

the fields that the view represents

(isWhitelist) Boolean

whitelist instance values

Throws:

Error - throws if a view already exists by the name field

getView(name) → {null|Object}

Return a view representation by name.

Source:
Parameters:
Name Type Description
name String

name of the view to get

Returns:
Type:
null | Object

set(data)

set model property declarations and runs schema validation and sets defaults

Model.set() is a helper method that's used to set data onto a Model. Using set() over direct Object assignment is advantageous because it aids in providing consistency. By using set() your validating the data (if you're using the schema option). If you're using none of the options for a model than direct assignment is perfectly suitable and set() is not necessary.

Source:
Parameters:
Name Type Description
data Object

toJSON() → {String}

converts index to an array and stringifies

Inherited From:
Source:
Throws:
TypeError
Returns:
Type:
String

toObject() → {Object}

converts a class to an object

Inherited From:
Source:
Returns:
Type:
Object