Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

brighthas-model

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brighthas-model

model for javascript

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Install

npm i brighthas/model --save

Create Model

var createModel = require("model");
var User = createModel("User",
	"name",
	"age",
	history,{type:"array"});

Config Model

User
 .attr("name")
 .attr("age")
 .attr("history",{type:"array"});

Add method


User.method("getUserInfo",function(){

	return "Name : " + this.name + "\n Age : " + this.age;

});


create Model object

var user = new User({ name:"leo" , age:20 , history: [] });

user.begin(); // begin update
user.name = "brighthas";
user.age = 30;
user.end(); // end update  , then may emit 'changed' event. 

update model


user.begin(); // begin update
user.name = "brighthas";
user.age = 30;
user.end(); // end update  , then may emit 'changed' event. 

console.log(this.hasError());  // true / false
console.log(this.errors);  //  [] is no error .

or

user.begin();
user.set({name:"leo",age:25});
user.end(); // end update  , then may emit 'changed' event. 
console.log(this.hasError());  // true / false
console.log(this.errors);  //  [] is no error .

or no use begin() & end()


user.name = 222 ;
console.log(this.hasError());  // true 

// if have error , then can't change other value.
user.age = 16 ;  
console.log(user.age) ; // no 16.

// so first clear errors.
user.errors = [];
user.age = 16; 
console.log(user.age); // 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 = createModel("T",ts:{type:"json"});

var t = T();

t.userInfo = {name:"leo"};

var u1 = t.userInfo;
var u2 = t.userInfo;

console.log(u1 === u2); // 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 = createModel("User",
	"name",
	"age",
	history,{type:"array"});


var T = createModel("T",user:{type:User}); // User is Model.

var t = T();

t.user = User({name:"leo",age:21});

var u1 = t.user; // the value is original value.
var u2 = t.user; // the value is original value.

console.log(u1 === u2); // true , 

or


function User(name,age,hisitory){
	this.name = name;
	this.age = age;
	this.history = hisitory || [];
}

User.prototype.toJSON = function(){
	return {
		name:this.name,
		age:this.age,
		history:this.history
	}
}

User.reborn = function(jsonObj){
	return new User(jsonObj.name,jsonObj.age,jsonObj.history);
}


//////////////////////////////////////////


var T = createModel("T",user:{type:User});

var t = T();

t.user = User("leo",21);

var u1 = t.user; // the value is original value.
var u2 = t.user; // the value is original value.

console.log(u1 === u2); // true , 

Keywords

FAQs

Package last updated on 22 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc