Skip to content
keyang xiang edited this page Apr 1, 2012 · 1 revision

#Model

##Register a Model

It is very simple to use mvc.regModel method to register a model:

	mvc.regModel({
		name:"student",
		proxy: new mvc.proxy.simpleData([
			{
				name:"Eric",
				age:"21"
			},
			{
				name:"Daniel",
				age:"23"
			}
		])
	});

The example above defined a model with name: student. It contains two data using simpleData proxy.

##Proxy A proxy indicates model how to retrieve, save, post, and delete data. For more details, please read Proxy in JQMVC.

##Filter and Sorter

It is able to add filters and sorters to a model while regisering.

	mvc.regModel({
		name:"student",
		fileter:function(data){
			if (data.age>22){
				return false;
			}else{
				return true;
			}
		},
		sorter:function(dataa,datab){
			if (dataa.age>datab.age){
				return 1;
			}else{
				return -1;
			}
		},
		proxy: new mvc.proxy.simpleData([
			{
				name:"Eric",
				age:"21"
			},
			{
				name:"Daniel",
				age:"23"
			}
		])
	});

Sorter and filter makes sense only when data is an Array.

##Extends Model

To add more methods to model, simply add "methods" key and your method functions while registering:

	mvc.regModel({
		name:"student",
		fileter:function(data){
			if (data.age>22){
				return false;
			}else{
				return true;
			}
		},
		sorter:function(dataa,datab){
			if (dataa.age>datab.age){
				return 1;
			}else{
				return -1;
			}
		},
		proxy: new mvc.proxy.simpleData([
			{
				name:"Eric",
				age:"21"
			},
			{
				name:"Daniel",
				age:"23"
			}
		]),
		methods:{
			getStudentNumber:function(callback){
				//implementation
			},
			findAStudent:function(callback){
				//implementation
			}
		}
	});

It is highly recommended to use callback function to pass result rather than a direct return value.

In those extended methods, use "this" keyword to refer to the model itself.

##Retrieve a model

To retrieve a registered model, just simply do:

	var studentModel=mvc.modelMgr.get("student");

##Load Data

	studentModel.load({},function(err,data){
		//implementation
	});

The first parameter is arguments passed to proxy. The callback also has two parameters: errro and data.

##Proxy actions

To perform a data action defined by proxy, for example a proxy that supports delete data in remote database, it is able to use "exec" method:

studentModel.exec("delete",{ studentNo:5 },function(err,res){

});

There are three arguments required:

1.Action Name: Action defined in proxy. It should be a function. 2.Argument: Argument that will be passed to the proxy. 3.Callback Function: Callback when action is done. It will return error and result.

Clone this wiki locally