Install
npm i brighthas/model --save
Create Model
var createModel = ;var User = ;
Config Model
User ;
Add method
Usermethod"getUserInfo"{ return "Name : " + thisname + "\n Age : " + thisage; };
create Model object
var user = name:"leo" age:20 history: ; user; // begin updateusername = "brighthas";userage = 30;user; // end update , then may emit 'changed' event.
update model
user; // begin updateusername = "brighthas";userage = 30;user; // end update , then may emit 'changed' event. console; // true / falseconsole; // [] is no error .
or
user;user;user; // end update , then may emit 'changed' event. console; // true / falseconsole; // [] is no error .
or no use begin() & end()
username = 222 ;console; // true // if have error , then can't change other value.userage = 16 ; console ; // no 16. // so first clear errors.usererrors = ;userage = 16; console; // 16
toJSON | reborn
var jsonObj = user.toJSON();
var user2 = User.reborn(jsonObj); console.log(user.name === user2.name); // true
user2.name = "brighthas"; console.log(user.name === user2.name); // false
Type
if no assign type , default type="string" .
Two types : value type
and complex type
.
Value type
string
| array
| json
| boolean
| number
| date
| regexp
when get value , the value is clone .
var T = ; var t = ; tuserInfo = name:"leo"; var u1 = tuserInfo;var u2 = tuserInfo; console; // false
Complex type
complex type can be Model
or object including Type.prototype.toJSON & Type.reborn function & emit changed
event.
when get value , the value is original value.
var User = ; var T = ; // User is Model. var t = ; tuser = ; var u1 = tuser; // the value is original value.var u2 = tuser; // the value is original value. console; // true ,
or
{ thisname = name; thisage = age; thishistory = hisitory || ;} Userprototype{ return name:thisname age:thisage history:thishistory } User{ return jsonObjnamejsonObjagejsonObjhistory;} ////////////////////////////////////////// var T = ; var t = ; tuser = ; var u1 = tuser; // the value is original value.var u2 = tuser; // the value is original value. console; // true ,