Represents domain-specific data or information that an application will be working with. A typical example is a user account (e.g name, avatar, e-mail) or a music track (e.g title, year, album). Holds information, but don’t handle behaviour and don’t format information or influence how data appears.
@cjssdk/model extends Emitter interface.
npm install @cjssdk/modelAdd the constructor to the scope:
var Model = require('@cjssdk/model');Create an empty instance:
var model = new Model();Create an instance with some data:
var model = new Model({
attr1: value1,
attr2: value2,
});Clear all data:
model.clear();emits
clearevent in case some data is present
Clear and set new model data:
model.init({
attr3: value3,
attr4: value4,
});can emit
clearandinitevents
Check an attribute existence:
if ( model.has('attr3') ) {
/* ... */
}Get a model attribute value by name:
var value = model.get('attr1');Update or create a model attribute:
var operationStatus = model.set('attr5', 'value5');emits
changeevent withprevfield in data in case of update operation
Delete the given attribute by name:
var operationStatus = model.unset('attr5');emits
changeevent
It is highly advisable to access a model data directly in case no events are required.
So instead of
var value = model.get('attr1');
model.set('attr5', 'value5');to avoid performance penalty it's better to use
var value = model.data.attr1;
model.data.attr5 = 'value5';There is a global var
DEVELOPwhich activates additional consistency checks and protection logic not available in release mode.
If you have any problems or suggestions please open an issue according to the contribution rules.
@cjssdk/model is released under the MIT License.