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

#Proxy

##Introduction

Proxy works as a bridge/adapter/connector between apps and data source. It is tied to data source and provides interfaces for app to perform remote data operations. For example, by default, JQMVC supports ajax proxy which allows apps to perform ajax call to remote and collect retrieved data.

Proxy is of lower layer of model. Thus it is almost transparent to most app development. However, it is still not difficult to develop a customised proxy.

##Develop a Proxy

This example shows how to develop a proxy that bridges app and local storage. A model using this proxy is able to load ,save, and delete local data in browser storage.

	mvc.ext(mvc.proxy, "localStorage", mvc.Class.create(mvc.cls.proxy, {
		load : function(params, callback) {
			var key=params.key;
			this.checkLib(params,function(err,res){
				if (err!=null){
					callback(err,null);
				}else{
					var res=localStorage.getItem(key);
					callback(null,res);
				}
			});
		},
		save:function(params,callback){
			var key=params.key;
			var val=params.val;
			this.checkLib(params,function(err,res){
				if (err!=null){
					callback(err,null);
				}else{
					localStorage.setItem(key,val);
					callback(null,true);
				}
			});
			
		},
		delete:function(params,callback){
			var key=params.key;
			this.checkLib(params,function(err,res){
				if (err!=null){
					callback(err,null);
				}else{
					localStorage.removeItem(key);
					callback(null,true);
				}
			
			});
		},
		checkLib:function(params,callback){
			var key=params.key;
			if (key==undefined || key==null || key==="" || !key){
				callback("Key is empty or not defined!",null);
			}else if (this.checkLib()===false){
				callback("Local Storage is not accessible! Check whether HTML5 is enabled.",null);
			}else{
				callback(null,true);
			}
		}
	}));

The proxy is always a child class of mvc.cls.proxy and generally it is located in mvc.proxy object.

for sentax of mvc.ext, please read Extend JQMVC.

User Proxy

To use a proxy just simply tie it to a model. It could be either tie the proxy to model while registering the model:

	mvc.regModel({
		name:"myModel",
		proxy:new mvc.proxy.localStorage()
	});

or use setProxy method in model to setup the proxy afterwards.

Now it is able to use the model to access browser local storage:

		//Save an item
		mvc.modelMgr.get("myModel").save({
			key:"hello",
			val:"world"
		},function(err,res){
			//result implementation here
		});
		
		//Load an item
		mvc.modelMgr.get("myModel").load({
			key:"hello"
		},function(err,res){
			//implementation here
		});
		
		
		//remove an item
		mvc.modelMgr.exec("delete",{
			key:"hello"
		},function(err,res){
			//implementation here
		});

Except for load and save methods which are built-in model methods, other self-defined methods in proxy like delete, update, searchBy etc, can be accessed through model.exec("methodName", parameters, callbackFunction).

Clone this wiki locally