Ext.data.Store采用Ext.data.proxy.Ajax获取数据后并进行处理

需求

使用Extjs中的Ext.data.Store时,利用Ext.data.proxy.Ajax的配置获取后台返回数据,想对返回的响应数据进行二次处理

解决方法

Ext.data.proxy.Ajax配置存在一个属性配置 extractResponseData(模板方法,以允许子类指定如何阅读获取响应。),参数response需要返回处理后的数据

API中截图如下
Ext.data.proxy.Ajax.extractResponseData
API内容

extractResponseData ( response ) : Object TEMPLATE PRIVATE
Template method to allow subclasses to specify how to get the response for the reader.
PARAMETERS
response :  Object
The server response
RETURNS :Object
The response data to be used by the reader
This is a template method. a hook into the functionality of this class. Feel free to override it in child classes.

响应参数结构
在这里插入图片描述

代码内容

写法一:

Ext.create('Ext.data.TreeStore', {
    model: 'treeModel',
    proxy: {
        type: 'ajax',
        method: 'POST',
        url: 'getMenuTreeData.action',
        reader: {
            type: 'json'
        },
        extractResponseData: function(response) {
            // 将响应的数据进行转换,具体的返回数据可以自行打印响应参数进行查看
            var result = Ext.util.JSON.decode(response.responseText);
            // 将处理后的数据,重新赋值给响应的数据
            response.responseText = Ext.util.JSON.encode(dealMenuTreeData(result))
            // 返回响应数据
            return response
        },
    },
    autoLoad: true
});

写法

Ext.create('Ext.data.TreeStore', {
    model: 'treeModel',
    proxy: {
        type: 'ajax',
        method: 'POST',
        url: 'getMenuTreeData.action',
        reader: {
            type: 'json'
        },
        extractResponseData: function(response) {
            // 将响应的数据进行转换,具体的返回数据,可以自行打印响应参数进行查看
            var result = Ext.util.JSON.decode(response.responseText);
            // 直接返回处理后的数据,我这里的数据是数组类型
            return dealMenuTreeData(result)
        },
    },
    autoLoad: true
});

参考

  1. https://www.jianshu.com/p/8e17ae3431ed
  2. http://t.zoukankan.com/lcchuguo-p-5207381.html

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注