Ext结合DWR的关键代码(运行成功的DWRProxy)

关键代码如下:

Store为:
  1. var ds = new Ext.data.Store({
  2.     proxy: new Ext.data.DWRProxy({
  3.         callback: Folder.getMessageList,
  4.          params: {
  5.              start: '0',
  6.              limit: PAGE_SIZE
  7.          }
  8.      }),
  9.     
  10.     //    proxy: new Ext.data.MemoryProxy(messageList),
  11.     
  12.     reader: new Ext.data.ObjectReader({
  13.         root: 'messages',
  14.         totalProperty: 'total'//用来让Ext的PagingToolbar组件分析总页数
  15.     }, [{
  16.         name: 'messageId'
  17.     }, {
  18.         name: 'from'
  19.     }, {
  20.         name: 'subject'
  21.     }, {
  22.         name: 'sendTime'
  23.     }, {
  24.         name: 'contentText'
  25.     }])
  26. });
  27. //在进行DWR请求之前,默认在请求参数中添加当前文件夹ID。这样DWR请求中包括的请求参数为:folderId,start,limit
  28. ds.on('beforeload'function(){
  29.     Ext.apply(this.baseParams, {
  30.         folderId: currentFolderId
  31.     });
  32. });
  33. ds.load({
  34.         params: {
  35.             start: currentStart,
  36.             limit: currentLimit
  37.         }
  38.     });//指定起始位置以及个数
自定义的DWRProxy和ObjectReader:
  1. Ext.data.DWRProxy = function(config){
  2.       Ext.data.DWRProxy.superclass.constructor.call(this);
  3.       Ext.apply(this, config ||
  4.       {});
  5.   };
  6.   
  7.   7Ext.extend(Ext.data.DWRProxy, Ext.data.DataProxy, {
  8.       //DWR请求
  9.       load: function(params, reader, callback, scope, arg){
  10.          currentStart = params.start;//保存当前页记录起始位置
  11.          currentLimit = params.limit;//保存当前页记录的个数
  12.          document.dwr = {
  13.              params: params,
  14.              reader: reader,
  15.              callback: callback,
  16.              scope: scope,
  17.              arg: arg
  18.          };
  19.          //处理请求参数,将各个请求参数转换到Array中
  20.          var callParams = new Array();
  21.          callParams.push(params.folderId);//当前文件夹ID
  22.          callParams.push(params.start);//请求的起始位置
  23.          callParams.push(params.limit);//请求的个数
  24.          if (this.params !== undefined && this.params !== null) {
  25.              this.callback.call(this, callParams, this.loadResponse);//DWR请求
  26.          }
  27.          else {
  28.              this.callback.call(thisthis.loadResponse);
  29.          }
  30.      },
  31.      //处理DWR返回
  32.      loadResponse: function(response){
  33.          var dwr = document.dwr;
  34.          try {
  35.              //IE throws an exception 'Error: TypeError, Object does not support this operation'   
  36.              //so use trycatch to suppress this exception   
  37.              delete document.dwr;//ie不支持delete
  38.          } 
  39.          catch (e) {
  40.          }
  41.          var result;
  42.          try {
  43.              result = dwr.reader.read(response);//读取请求返回的json
  44.          } 
  45.          catch (e) {
  46.              //this.fireEvent("loadexception",this,dwr,response,e);   
  47.              dwr.callback.call(dwr.scope, null, dwr.arg, false);
  48.              return;
  49.          }
  50.         dwr.callback.call(dwr.scope, result, dwr.arg, true);
  51.      },
  52.      failure: function(errorString, exception){
  53.          console.log("DWR " + exception);
  54.     },
  55.      update: function(params, records){
  56.      }
  57.  });
  58.  //自定义一个用于处理返回消息列表的Reader
  59.  Ext.data.ObjectReader = function(meta, recordType){
  60.      Ext.data.ObjectReader.superclass.constructor.call(this, meta, recordType);
  61.  };
  62.  Ext.extend(Ext.data.ObjectReader, Ext.data.DataReader, {
  63.      //处理DWR返回
  64.      read: function(response){
  65.          var responseDecode = Ext.util.JSON.decode(response);//注意,由java的json-lib生成的json串需要解码一下
  66.          var data = responseDecode.messages;
  67.          var sid = this.meta ? this.meta.id : null;
  68.          var recordType = this.recordType, fields = recordType.prototype.fields;
  69.         var records = [];
  70.          var root = data;
  71.         for (var i = 0; i < root.length; i++) {
  72.              var obj = root[i];
  73.              var values = {};
  74.              var id = obj[sid];
  75.              for (var j = 0, jlen = fields.length; j < jlen; j++) {
  76.                  var f = fields.items[j];
  77.                  var k = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
  78.                  var v = null;
  79.                 var idx = k.indexOf(".");
  80.                  if (idx == -1) {
  81.                      v = obj[k] !== undefined ? obj[k] : f.defaultValue;
  82.                  }
  83.                  else {
  84.                      var k1 = k.substr(0, idx);
  85.                      var k2 = k.substr(idx + 1);
  86.                      if (obj[k1] == undefined) {
  87.                          v = f.defaultValue;
  88.                      }
  89.                      else {
  90.                          var obj2 = obj[k1];
  91.                          v = obj2[k2] !== undefined ? obj2[k2] : f.defaultValue;
  92.                     }
  93.                  }
  94.                  v = f.convert(v);
  95.                  values[f.name] = v;
  96.              }
  97.              var record = new recordType(values, id);
  98.             records[records.length] = record;
  99.         }
  100.         return {
  101.             records: records,//返回的消息列表记录
  102.             totalRecords: responseDecode.total//总个数
  103.         };
  104.     }
  105. });
posted @ 2010-05-07 23:30  貔貅  阅读(567)  评论(0编辑  收藏  举报