@sugarcoated/fondant-model
v1.0.5
Published
Model data without the hassle.
Downloads
4
Readme
We all model data, but we have nothing to cling on to for managing the data within that model. It is just an object that is returned from an API, or a database. Converting plain objects to types or updating the contents of a typed model without losing reference is strenuous, which is where Model
comes in. Extending this class will inherit important methods for cloning, assigning, casting and so on.
- View the source at src/Globals/Model.js
- View the test fie at test/Globals/testModel.js
- View the NPM package at @sugarcoated/fondant-dictionary
API Reference
new Model()
Creates a Model
instance.
class Book extends Model { constructor (bookName) { super ();
this.bookName = bookName;
}
}
.difference(comparison)
Checks the difference against the instance and the Object
passed.
comparison
is expected as anObject
, representing a set ofm properties to compare against.myModel.difference(myObject);
.assign(values)
Assigns values from an Object
to the properties on an instance.
values
is expected as anObject
, representing a casted or uncasted version of the instance with differing values to assign to the instance.myModel.assign(itClone);
.prune([item])
Prunes foreign* properties (not prefixed with the name of the model) from an instance, or a passed Object
.
item
is an optional parameter, expected as anObject
. When noitem
is passed, pruning applies to the instance called upon.itBook.prune();
.clone()
Creates a direct copy of the modeled Object
. On this base class, this will simply return a plain Object
. When extending the Model
class, an override can be written to cast this as seen fit.
const myClone = myModel.clone();
.cast(item, [model])
Casts a plain Object
to an instance of Model
, or passed in. An override can be written for this method to pass in the Class
itself, and return the super
method's result anyway.
item
is expected as anObject
, representing an uncasted identical version of the model.model
is an optional parameter, expected as aClass
thatextends
Model
.static cast (castItem) { return super.cast(castItem, User); }