Model

Model

For the most part this is initially a js-schema object, which gets transformed into a validation-controlled data object.

This conversion has to happen after the constructor finishes, which means we can't do this as part of the Model class's constructor, because that finishes before the full class hierarchy instantiation has finished.

As such, you never want to instantiate a model manually, and always want to use either Model.create(), or Model.load(), both of which fall through to the Models class, which is essentially a factory class with additional functionality bolted on.

Classes

Model

Members

ALLOW_INCOMPLETE

Source:

This symbol can be used to create models with missing "required" fields. Note that this value is inherited, and so can be referenced from your own model's name rather than using Model.ALLOW_INCOMPLETE.

E.g.


  class Something extends Model {
    // ...
  }

  Something.create(undefined, Something.ALLOW_INCOMPLETE);

Methods

(static) create(allowIncompleteopt) → {Model}

Source:

Create an instance of this model, with an optional data object that will be used to bootstrap the model's fields.

Note that this data must be schema-conformant for this model, or this function will throw.

If a backend store is used, this function will run async, returning a Promise that can be awaited, or handled with .then()

When no backend is used, this function will run synchronously.

Parameters:
Name Type Attributes Description
allowIncomplete Symbol <optional>
allows models to be created without specifying all required data, if set to Model.ALLOW_INCOMPLETE.
Throws:
See Models.create for pass-through throws.
Returns:
an instance of this model
Type
Model

(async, static) load(recordName) → {*}

Source:

Load a stored record that uses this model from the back end.

Note that this will throw if:

  • there is no stored record to load
  • the stored record is not valid JSON
  • the stored record is not schema-conformant
Parameters:
Name Type Description
recordName *
Throws:
one of several errors
Type
*
Returns:
a stored model instance
Type
*

(async) delete()

Source:

Delete this model from the backend.

Throws:
one of several errors
Type
*

get(pathkey) → {*}

Source:
get a value by pathkey, rather than direct assignment
Parameters:
Name Type Description
pathkey *
Returns:
the pathkey-associated value
Type
*

reset(postResetPayload)

Source:
Recursively reset this model to default values, for any value that has a default set in its associated schema.
Parameters:
Name Type Description
postResetPayload * the data with which to bootstrap this model after resetting.

(async) save()

Source:

Save this model instance to the backend. Note that this requires the model class to specify a __meta.recordName field, which must indicate which key path to use as "primary key" equivalent, or be a function that, given a model, yields a string to be used as record key.

Throws:
one of several errors
Type
*

set(pathkey, value)

Source:
set a value by pathkey, rather than direct assignment
Parameters:
Name Type Description
pathkey *
value *

toForm(options) → {*}

Source:
See:

Generate a node tree for working with this model's data in some non-HTML context. By default, this yields the (P)React equivalent of a <form>, with options.onSubmit being used for submission handling.

See tree.createFormTree for details on what the available options are.

Parameters:
Name Type Description
options object a configuration object
Returns:
a JS object tree representing a form
Type
*

toHTMLForm(options) → {String}

Source:
See:
Generate a <form> element for viewing/updating this model. See html.createFormHTML for details on what the available options are.
Parameters:
Name Type Description
options object a configuration object
Returns:
form HTML string data
Type
String

toHTMLTable(options) → {String}

Source:
See:
Generate a <table> with form fields for this model. See html.createTableHTML for details on what the available options are.
Parameters:
Name Type Description
options object a configuration object
Returns:
table HTML string data
Type
String

toHTMLTableRows(options) → {String}

Source:
See:
Generate an array of <tr> elements with form fields for this model. See html.createTableRowHTML for details on what the available options are.
Parameters:
Name Type Description
options object a configuration object
Returns:
table rows HTML string data
Type
String

toString() → {*}

Source:
Yield formatted JSON, with alphabetically sorted keys, with all defaults omitted (as they are not enumerable).
Returns:
a JSON string with sorted keys
Type
*

toTable(options) → {*}

Source:
See:
  • tree.createTableTree

Generate a node tree for working with this model's data in some non-HTML context. By default, this yields the (P)React equivalent of a <table> for templating into a component render.

See tree.createTableTree for details on what the available options are.

Parameters:
Name Type Description
options object a configuration object
Returns:
a JS object tree representing a table
Type
*

toTableRows(options) → {*}

Source:
See:

Generate an array of table rows for working with this model's data in some non-HTML context. By default, this yields an array of the (P)React equivalent of <tr>, for templating into a component render.

See tree.createTableTree for details on what the available options are.

Parameters:
Name Type Description
options object a configuration object
Returns:
a JS array representing a set of table rows
Type
*

updateFromSubmission(data)

Source:

Update this model from a form submission, or any data payload that encodes the model data using a flat : format, e.g. a format which encodes this object:


{
  prop1: val1,
  prop2: {
    prop3: val2,
    prop4: {
      prop5: val3
    }
  }
}

as this flat object:


{
  "prop1": "val1",
  "prop2.prop3": "val2",
  "prop2.prop4.prop5": "val3",
}
Parameters:
Name Type Description
data *
Throws:
one of several errors
Type
*

valueOf() → {*}

Source:
Yield a fully qualified plain object, with default values included.
Returns:
a plain JS object
Type
*