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

DWR 专栏收录该内容
1 篇文章 0 订阅

在使用Ext与DWR框架时,我们往往会用到GridPanel组件搭配PagingToolbar组件来实现翻页数据列表。翻页的时候每一页都要从后台获取该页的数据列表信息。
在解决此问题时,花了不少时间,看过不少前人的代码,终于成功了!共享之。
关键代码如下:

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
        }
    });//指定起始位置以及个数



自定义的DWRProxy和ObjectReader:

 

Ext.data.DWRProxy = function(config){
    Ext.data.DWRProxy.superclass.constructor.call(this);
    Ext.apply(this, config ||
    {});
};

Ext.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//总个数
        };
    }
});

 

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值