写个适合自己的RPC Engine

学习了Swato之后,受到启发,对Zigzag Chen的代码作了点修改,写了个适合自己的RPC Engine:
 
  1. // imports dependencies:  
  2. GRS.loadJS("../jslib/3rdparty/prototype.js");  
  3. GRS.loadJS("../jslib/3rdparty/json.js");  
  4.   
  5. Ext.namespace("GRS.rpc");  
  6. /** 
  7.  * Constructor of GRS.rpc.Engine 
  8.  * @param url 
  9.  * @param options 
  10.  * @author Zhijie Chen <[email protected]> 
  11.  * @author Sam Chen (inspired by Zigzag Chen's swato-engine.js) 
  12.  * @version 1.0 11/22/2007 
  13.  */  
  14. GRS.rpc.Engine = function(url,options) {  
  15.    this.url = url;  
  16.    this.conn_id = Math.floor(Math.random()*100000000);  
  17.    this.options = options;  
  18.    GRS.rpc.Engine.superclass.constructor.call(this)  
  19. }  
  20. Ext.extend(  
  21.    GRS.rpc.Engine,  
  22.    Ext.util.Observable,  
  23.    {call: function(catcher,serviceDotMethodName,params,options) {  
  24.        if ( this.url == null ) return;  
  25.        var serviceId=serviceDotMethodName.substring(0,serviceDotMethodName.indexOf("."));  
  26.        var methodName=serviceDotMethodName.substring(serviceDotMethodName.indexOf(".")+1,serviceDotMethodName.length);  
  27.        var option=new Object();  
  28.        Object.extend(option,options);  
  29.        option.onComplete=this._onRequestComplete.bind(catcher);  
  30.        new Ajax.Request(this.url+"/"+serviceId, this._requestOptions(serviceId.hashCode||serviceId,methodName,params,option));  
  31.     },  
  32.     fetch: function(catcher,url,options) {  
  33.        if ( url == null ) return;  
  34.        var option=new Object();  
  35.        Object.extend(option,options);  
  36.        option.onComplete=this._onRequestComplete.bind(catcher);  
  37.        new Ajax.Request(url,option);  
  38.     },    
  39.     send: function(catcher,jsObject,remoteObjectID,options) {  
  40.        this.call(catcher,remoteObjectID,'',[jsObject],options);  
  41.     },  
  42.     _requestOptions: function(remoteObjectID,methodName,params,options) {  
  43.        var sendingData={  
  44.           "params":params,  
  45.           "method":methodName,  
  46.           "id":remoteObjectID,  
  47.           "conn_id":this.conn_id};  
  48.        var requestOptions=new Object();  
  49.        Object.extend(requestOptions,this.options);  
  50.        Object.extend(requestOptions,options);  
  51.        requestOptions.parameters=JSON.stringify(sendingData);  
  52.        return requestOptions;  
  53.     },  
  54.     _onRequestComplete : function(response) {  
  55.        if (response.status != 200) {  
  56.           return;  
  57.        }  
  58.        eval("var error = " + response.responseText.error);  
  59.        if (error!=undefined) {  
  60.           if (this.failure!=null) {  
  61.              // call the failure handler  
  62.              this.failure.apply(this, [error]);  
  63.           } else {  
  64.              alert("Warning from rpc.js : Error occurred and no error handler defined.");  
  65.           }  
  66.        } else {  
  67.           if (this.success!=null){  
  68.              // call the success handler:  
  69.              this.success.apply(this, [response.responseText]);  
  70.           }  
  71.        }  
  72.     }  
  73.    }  
  74. );  
  75.   
  76. // global rpcengine  
  77. rpcengine=new GRS.rpc.Engine("/RPCServlet");  

简单的测试代码:
 
  1. <script type="text/javascript" charset="UTF-8" src="../../3rdparty/ext-2.0-beta1/adapter/ext/ext-base.js"></script>  
  2. <script type="text/javascript" charset="UTF-8" src="../../3rdparty/ext-2.0-beta1/ext-all.js"></script>  
  3.   
  4. <script type="text/javascript" charset="UTF-8" src="../grsframework-core.js"></script>    
  5. <script type="text/javascript" charset="UTF-8" src="../rpc.js"></script>  
  6.   
  7. <script>  
  8. var handler={  
  9.     success:function(ret){  
  10.         alert("testRPC.html :: Result :: " + ret);  
  11.     },  
  12.     failure:function(err){  
  13.         alert("Err occurred : " + err);  
  14.     }  
  15. };  
  16. window.onload = function() {  
  17.    alert("testing rpcengine...");  
  18.    rpcengine.call(handler,"userManager.getUserList",[]);  
  19. }  
  20. </script>  

你可能感兴趣的:(JavaScript,json,Ajax,ext,Gmail)