关键代码如下:
Store为:
- var ds = new Ext.data.Store({
- proxy: new Ext.data.DWRProxy({
- callback: Folder.getMessageList,
- params: {
- start: '0',
- limit: PAGE_SIZE
- }
- }),
- // proxy: new Ext.data.MemoryProxy(messageList),
- reader: new Ext.data.ObjectReader({
- root: 'messages',
- totalProperty: 'total'//用来让Ext的PagingToolbar组件分析总页数
- }, [{
- name: 'messageId'
- }, {
- name: 'from'
- }, {
- name: 'subject'
- }, {
- name: 'sendTime'
- }, {
- name: 'contentText'
- }])
- });
- //在进行DWR请求之前,默认在请求参数中添加当前文件夹ID。这样DWR请求中包括的请求参数为:folderId,start,limit
- ds.on('beforeload', function(){
- Ext.apply(this.baseParams, {
- folderId: currentFolderId
- });
- });
- ds.load({
- params: {
- start: currentStart,
- limit: currentLimit
- }
- });//指定起始位置以及个数
- Ext.data.DWRProxy = function(config){
- Ext.data.DWRProxy.superclass.constructor.call(this);
- Ext.apply(this, config ||
- {});
- };
- 7Ext.extend(Ext.data.DWRProxy, Ext.data.DataProxy, {
- //DWR请求
- load: function(params, reader, callback, scope, arg){
- currentStart = params.start;//保存当前页记录起始位置
- currentLimit = params.limit;//保存当前页记录的个数
- document.dwr = {
- params: params,
- reader: reader,
- callback: callback,
- scope: scope,
- arg: arg
- };
- //处理请求参数,将各个请求参数转换到Array中
- var callParams = new Array();
- callParams.push(params.folderId);//当前文件夹ID
- callParams.push(params.start);//请求的起始位置
- callParams.push(params.limit);//请求的个数
- if (this.params !== undefined && this.params !== null) {
- this.callback.call(this, callParams, this.loadResponse);//DWR请求
- }
- else {
- this.callback.call(this, this.loadResponse);
- }
- },
- //处理DWR返回
- loadResponse: function(response){
- var dwr = document.dwr;
- try {
- //IE throws an exception 'Error: TypeError, Object does not support this operation'
- //so use trycatch to suppress this exception
- delete document.dwr;//ie不支持delete
- }
- catch (e) {
- }
- var result;
- try {
- result = dwr.reader.read(response);//读取请求返回的json
- }
- catch (e) {
- //this.fireEvent("loadexception",this,dwr,response,e);
- dwr.callback.call(dwr.scope, null, dwr.arg, false);
- return;
- }
- dwr.callback.call(dwr.scope, result, dwr.arg, true);
- },
- failure: function(errorString, exception){
- console.log("DWR " + exception);
- },
- update: function(params, records){
- }
- });
- //自定义一个用于处理返回消息列表的Reader
- Ext.data.ObjectReader = function(meta, recordType){
- Ext.data.ObjectReader.superclass.constructor.call(this, meta, recordType);
- };
- Ext.extend(Ext.data.ObjectReader, Ext.data.DataReader, {
- //处理DWR返回
- read: function(response){
- var responseDecode = Ext.util.JSON.decode(response);//注意,由java的json-lib生成的json串需要解码一下
- var data = responseDecode.messages;
- var sid = this.meta ? this.meta.id : null;
- var recordType = this.recordType, fields = recordType.prototype.fields;
- var records = [];
- var root = data;
- for (var i = 0; i < root.length; i++) {
- var obj = root[i];
- var values = {};
- var id = obj[sid];
- for (var j = 0, jlen = fields.length; j < jlen; j++) {
- var f = fields.items[j];
- var k = (f.mapping !== undefined && f.mapping !== null) ? f.mapping : f.name;
- var v = null;
- var idx = k.indexOf(".");
- if (idx == -1) {
- v = obj[k] !== undefined ? obj[k] : f.defaultValue;
- }
- else {
- var k1 = k.substr(0, idx);
- var k2 = k.substr(idx + 1);
- if (obj[k1] == undefined) {
- v = f.defaultValue;
- }
- else {
- var obj2 = obj[k1];
- v = obj2[k2] !== undefined ? obj2[k2] : f.defaultValue;
- }
- }
- v = f.convert(v);
- values[f.name] = v;
- }
- var record = new recordType(values, id);
- records[records.length] = record;
- }
- return {
- records: records,//返回的消息列表记录
- totalRecords: responseDecode.total//总个数
- };
- }
- });