【问题标题】:ExtJS 4 store callback function don't get recordExtJS 4 存储回调函数不获取记录
【发布时间】:2018-12-19 03:54:23
【问题描述】:

存储回调如下:

var store = new Ext.create("Ext.data.TreeStore", {
    model: "DeptModel",
    proxy: {
        type: 'ajax',
        url: 'jsonWebCommMenuOp_getMenus?PID=0',
        reader: 'json'
    },
    autoload: true
});

store.load({
    callback: function(records, options, success) {
        var jsonStr = Ext.JSON.encode(records[0].raw);
        var jsonObj = Ext.JSON.decode(jsonStr);
        alert(jsonStr);
    }
});

服务器响应如下:

{
    "success": true,
    "msg": "123"
}

我测试回调参数success,它等于true,但records是空白[]。

这是什么原因?请帮帮我。

【问题讨论】:

  • 你的DeptModel在哪里?

标签: extjs callback extjs4 store


【解决方案1】:

在您的情况下,您必须考虑以下事项:

  • 您的服务器响应究竟是什么。如果是{"success":true, "msg":"123"},那你就真的错过数据了。
  • 您必须在reader config 中设置包含数据项的属性名称。在 ExtJS 4 中,这可以使用 root 来完成,在 EXTJS 5,6 中使用 rootProperty
  • 如果您通过代码加载存储数据,请将autoLoad 设置为false

ExtJS 4.2

reader: {
    type: 'json',
    root: 'data'
}   

ExtJS 5 和 ExtJS 6

reader: {
    type: 'json',
    rootProperty: 'data'
}   

注意事项:

我已经用 EXTJS 4.2 复制了您的测试用例,这些是导致这种行为的原因:

  • JSON 响应不包含数据

  • root (rootProperty) 配置未设置。

  • JSON 响应包含数据,但包含该数据的属性名称与 reader 配置中的名称不同。

工作示例:

服务器响应:

{
    "success": true,
    "data": [
        {"text": "Some words"},
        {"text": "Some words"},
        {"text":"Some words"}
    ]
}

HTML:

Ext.define('DeptModel', {
    extend: 'Ext.data.Model',
    fields: [
        {name: 'text', type: 'string'}
    ]
});
Ext.onReady(function(){
    var store = new Ext.create("Ext.data.TreeStore", {
        model: "DeptModel",
        proxy: {
            type: 'ajax',
            //url: 'jsonWebCommMenuOp_getMenus?PID=0',
            // I've used simple PHP script to populate JSON response:
            url: 'store-load.php',
            reader: {
                type: 'json',
                root: 'data'
            }   
        },
        autoLoad: false
    });

    store.load(
    {
        callback: function(records, options, success)
        {
            //var jsonStr = Ext.JSON.encode(records[0].raw);
            //var jsonObj = Ext.JSON.decode(jsonStr);
            //alert(jsonStr);
            console.log(records);
        }
    }); 

});

【讨论】:

    【解决方案2】:

    您需要在以下几点重新检查您的代码:-

    1. autoLoad 而不是 autoload
    2. TreeStore 你应该提到root: {}

    看看这个working fiddle

    注意:我使用data.json来请求。

    希望这将帮助/指导您正确。

    【讨论】:

      猜你喜欢
      • 2013-02-13
      • 2011-05-02
      • 2013-04-27
      • 2013-02-14
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多