2008-03-31

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

关键字: ajax
Ext.namespace("GRS.framework.rpc");
/**
 * GRS.framework.rpc.RPCEngine
 * 
 * @param url
 * @param options
 * @author Zhijie Chen <zigzag.chen@gmail.com>
 * @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");
评论
发表评论

提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则

您还没有登录,请登录后发表评论

sam.ds.chen
搜索本博客
博客分类
我的相册
04352697-7823-382a-9592-e177ec21889b-thumb
MyDesktop.zh
共 17 张
最近加入圈子
存档
最新评论