Promised Models
Key features
- promise based
- typed attributes
- nested models and collections
- async calculations and validation
Install
$npm install --save promised-models
Usage
var Model = FashionModel = attributes: name: ModelattributeTypesString model = name: 'Kate' ; model; // 'Kate'
Api reference (in progress)
Model sync methods
Model.inherit(properties, [classPorperties])
inherit Creates you own model class by extending Model
. You can define attributes, instance/class method and properties. Inheritance is built over inherit.
var CountedModels = Model;
Model.attributeTypes
attributeTypes Namespace for predefined types of attributes. Supported types:
String
Number
Boolean
List
— for storing arraysModel
— for nested modelsModelsList
— for nested collectionsObject
— serializable objects
You can extend default attribute types or create your own
var DateAttribute = ModelattributeTypesNumberFashionModel = Model;
Note: models.attributes
will be replaced in constructor with attribute instances.
var model = ;modelattributesbirthDate instanceof DateAttribute; //true
model.set(attributeName, value)
set Set current value of attribute.
var model = ;model;modelattributesname;model;
Note: setting null
is equivalent to call .unset()
model.get(attributeName)
get Get current value of attribute.
var model = name: 'Kate' birthDate: 1974 1 16model; //Katemodelattributesname; //Katemodel; //throws error as unknown attribute
model.toJSON()
toJSON Return shallow copy of model data.
Note: You can create internal attributes, which wouldn't be included to returned object.
var FashionModel = attributes: name: ModelattributeTypesString sename: ModelattributeTypesString fullName: ModelattributeTypesString model = name: 'Kate' sename: 'Moss' fullName: 'Kate Moss';model; // {fullName: 'Kate Moss'}model; // Kate
Note: Returned object supposed to be serializable via JSON.parse()
. Due to this reason NaN
and Infinity
are serialized in this way:
NaN -> null
Infinity -> 'Infinity'
model.isChanged([branch])
isChanged Has model changed since init or last commit/save/fetch.
var FashionModel = Model model = name: 'Kate' weight: 55 ;model; //falsemodel;model; //true
model.commit([branch])
commit Cache current model state
var model = ;model;model;//truemodel;model;//false
model.revert([branch])
revert Revert model state to last cashed one
var model = name: 'Kate' weight: 55;model;model;model; //55model; //false
Note: You can create your own cache by passing branch param.
var RENDERED = 'RENDERED';model;
model.on([attributes], events, cb, [ctx])
on Add event handler for one or multiple model events.
List of events:
change
– some of attributes have been changedchange:attributeName
–attributeName
have been changeddestruct
– model was destructedcalculate
– async calculations started
model ;
model.un([attributes], events, cb, [ctx])
un Unsubscribe event handler from events.
//subscribemodel; //unsubscribemodel;
model.destruct()
destruct Remove all events handlers from model and removes model from collections
model.isSet(attributeName)
isSet Returns true
if attribute was set via constructor or set
var model = ;model; //falsemodel;model; //true
model.unset(attributeName)
unset Set attribute to default value and model.isSet() === 'false'
var model = ;model;model;model; //falsemodel; //empty string (default value)
Model async methods
model.validate()
validate Validate model attributes.
var FashionModel = Model model = ; model;
model.ready()
ready Fulfils when all calculations over model finished.
var FashionModel = Model model = ; model;model;
model.fetch()
fetch Fetch data associated with model from storage.
var FashionModel = Model model = id; model;
model.save()
save var FashionModel = Model model = ; model;model
model.remove()
remove Removes model from storage.
Model additional methods and properties
model.isNew()
model.isReady()
model.trigger(event)
model.calculate()
model.CHANGE_BRANCH
model.CALCULATIONS_BRANCH
These methods provided for advanced model extending. Consult source for details.
Model static methods and properties
Model.Storage
Storage Abstract class for model storage
var FashionModel = Model;
Model.storage
Class storage Storage class
var SuperModel = FashionModel;
Model.Attribute
Attribute Base class for model attribute
var CustomAttribute = Modelattribute
Model.attributes
Class attributes Model class attributes
var SuperModel = FashionModel;
Model.on([attributes], events, cb, [ctx])
Bind event on all models of class
FashionModel;
Model.un([attributes], events, cb, [ctx])
Unbind event on all models of class
List
Array like object returned for fields types List
and ModelsList
var Podium = Model.inherit({
attributes: {
models: Model.attributeTypes.ModelsList(FashionModel)
}
}),
podium = new Podium(data),
list = podium.get('models'), //instanceof List
model = list.get(0); //instanceof Model
Mutating methods
List inerits Array mutating methods: pop
, push
, reverse
, shift
, sort
, splice
, unshift
podium.get('models').push(new FashionModel());
list.get(index)
Get list item by index
podium.get('models').get(0);// instanceof Model
list.length()
Returns length of list
list.toArray()
Returns shallow copy of Array, wich stores List items
podium.get('models').forEach(function (model) {
model; // instanceof Model
});
Model.ValidationError
ValidationError Error class for validation fail report
run tests
$ npm test