1.0.5 • Published 6 years ago

@sugarcoated/fondant-model v1.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

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.

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 an Object, 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 an Object, 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 an Object. When no item 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 an Object, representing an uncasted identical version of the model.
  • model is an optional parameter, expected as a Class that extends Model.

    static cast (castItem) { return super.cast(castItem, User); }

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago