TOC
moldy
Create a Moldy.
var personMoldy = Moldy ;personMoldyshouldhavea;personMoldyshouldhavea;personMoldyshouldhavea;
Property Attributes
Type & default
Properties can by strongly typed. If a type has been defined, values are cast to that type automatically. If a value cannot be cast to a type then the value will be set to null
or the default
if it has been defined.
var personMoldy = Moldy; /** * When a model's properties have been `typed` the assigned values are cast on the fly * to ensure the model's data remains sanitized. */ /** * Cast a `string` for `age` to a `number` */ personMoldyage = '13'; personMoldyageshouldandbean; /** * Cast a truthy `string` for `active` as a `boolean` */ personMoldyactive = 'yes'; personMoldyactiveshouldandbean; /** * `active` is typed as a `boolean` _and_ a `default` has been defined. When an * assigned value that cannot be cast as a `boolean` is set then the `default` will * apply. */ personMoldyactive = 'this is not a boolean'; andbean; /** * Cast a `string` for `tags` as an `array` */ personMoldytags = 'lorem'; andbean;
Optional
Properties can be optional. By making a property optional, isValid()
and toJson()
will ignore it if is has not been set.
var personMoldy = Moldy ;/** * To ensure this `person` is valid we only need to set the `id` and `name` because * the other keys are either `optional` or have `defaults`. */personMoldyid = 1;personMoldyname = 'David';personMoldyshouldbeok;
Arrays of a type
A property can be defined as array
of a type like an array
of strings
, or an array
of numbers
.
var personMoldy = Moldy ;/** * When defining an array of a type, the arrays are normal arrays however they have been * extended to allow hooks into the necessary methods for sanitization. */personMoldytagsshouldbeanArray;personMoldytagsshouldhaveaandbeaNumber;personMoldytagsshouldhaveaandbeaFunction;personMoldytagsshouldhaveaandbeaFunction;personMoldytagsshouldhaveaandbeaFunction;personMoldytagsshouldhaveaandbeaFunction;personMoldytagsshouldhaveaandbeaFunction;personMoldytagsshouldhaveaandbeaFunction;personMoldytagsshouldhaveaandbeaFunction;/** * Pushing a value - like normal */personMoldytags;/** * We are pushing a `number` here to show how the value will be cast to a string */personMoldytags;/** * The value `1` is now a string */personMoldytags 1 should;personMoldytagsshouldhavea;personMoldytagsshould;/** * A gotcha for using primitive types in this context is that they are not sanitized * based on the schema if they are changed directly */personMoldytags 1 = 1;/** * Technically this should have cast the number `1` to a string but it was a design * decision not to add getters/setters to each item in an array. A santize method will * be added in the next version */personMoldytags 1 should;
Array types can also be model schemas.
var personMoldy = Moldy ;/** * Note, we are missing the `model` key and the `year` is a string */personMoldycars;personMoldycars 0 should;
A model's url aka endpoint
A url (endpoint) is automatically generated based on the Moldy
name, key, $url()
and $baseUrl()
.
var Person = Moldy;var personMoldy = Person;Personshould;/** * The url can be changed using either `base()` or `url()` */Person;Personshould;
find
To find by id
or key
, give an object with appropriate conditions.
var personMoldy = Moldy;personMoldy;
If an adapter responds with an array the first item will be returned.
var Person = Moldy;/** * In this example the end point GET `http://localhost:3000/api` returns an array of items. * Moldy will return the first item out of the array. If you need to return an array you can * use the $collection method. */Person;
find
To find an array of models.
var Person = Moldy;Person;
save
To save the model, call save()
. If the model is dirty
(has not been saved to the server and therefore does not have a valid key
) then the http method will be POST. If the model has been saved, then the http method will be PUT.
var personMoldy = Moldy;personMoldyname = 'David';personMoldy;
destroy
To destroy a model, call destroy()
.
var personMoldy = Moldy;personMoldyname = 'David';personMoldy;