写个适合自己的RPC Engine (续)

Ext.namespace("GRS.framework.rpc");
/**
 * GRS.framework.rpc.RPCEngine
 * 
 * @param url
 * @param options
 * @author Zhijie Chen <[email protected]>
 * @author Sam Chen (inspired by Zigzag Chen's swato-engine.js)
 * @version 1.0 11/22/2007
 * @version 1.1 11/26/2007
 * @version 1.2 12/07/2007
 * @version 1.3 12/26/2007
 * @version 1.4 03/29/2008 (should be final)
 */
GRS.framework.rpc.RPCEngine = function(url,options) {
   this.url = url;
   this.conn_id = Math.floor(Math.random()*100000000);
   this.options = options;
   GRS.framework.rpc.RPCEngine.superclass.constructor.call(this)
}
Ext.extend(
   GRS.framework.rpc.RPCEngine,
   Ext.util.Observable,
   /**
    * @public
    * @param callback the callback function that processes the reponse or response.responseText
    * @param serviceDotMethodName the remote source requested
    * @param params (Array) the parameters
    * @param options the options
    */
   {call: function(callback, serviceDotMethodName, params, options) {
       if (this.url == null) {
       	  Ext.MessageBox.show({
             title: 'Error',
             msg: 'Cannot make rpc request as url is not defined.',
             buttons: Ext.MessageBox.OK,
             icon: 'ext-mb-error'});
          return;
       }      
       var serviceName = serviceDotMethodName.substring(0,serviceDotMethodName.indexOf("."));
       var methodName = serviceDotMethodName.substring(serviceDotMethodName.indexOf(".") + 1, serviceDotMethodName.length);
       _onComplete = function(response){
       	  Ext.MessageBox.hide();
          eval("var rt = " + response.responseText);
          var error = rt.error;
          if (error) {
             // session timed out -> relogin
             if (Constants.MSG_CODE_LOGIN_RELOGIN == error.code) {
             	Ext.MessageBox.show({
                   title: 'Error',
                   msg: error.msg,
                   buttons: Ext.MessageBox.OK,
                   icon: 'ext-mb-error'});
                // TODO: clear password
             	GRS.loginDlg.show();
             } else {
                Ext.MessageBox.show({
                   title: 'Error',
                   msg: error.msg + '<br>Please contact the system administrator.',
                   buttons: Ext.MessageBox.OK,
                   icon: 'ext-mb-error'});
             }
             return
          }
          if (options && options.returnResponse == true) {
             callback(response);
      	  } else {
             callback(rt);
      	  }	 
       }
       
       Ext.Ajax.request({
          url: this.url,
          params: JSON.stringify({
             'service': serviceName,
             'method': methodName,
             'params': params,
             'conn_id': this.conn_id}),
          success: _onComplete,
          failure: function(){
             Ext.MessageBox.show({
                title: 'Error',
                msg: 'The requested resource ' + this.url + ' is not available.<br>Please contact the system administrator.',
                buttons: Ext.MessageBox.OK,
                icon: 'ext-mb-error'})
          },
          scope: this});
    }
   }
);

// global rpcengine
rpcengine = new GRS.framework.rpc.RPCEngine("GRS_AJAX_RPC");

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