ExtJS 6 访问消息属性在存储同步的回调

ExtJS 6 access messageProperty in store sync's callback

本文关键字:存储 同步 回调 属性 访问 消息 ExtJS      更新时间:2023-09-26

我想在同步 ExtJS 网格后在 UI 中显示来自服务器的消息。以下是这是如何进行的

摘录:
this.store.sync({
        callback: function (records, operation, success) {
          // messageProperty accessing code
        },
        success: function (batch, options) {
          // messageProperty accessing code
        },
        failure: function (batch, options) {
        }
    });

下面是商店定义的一部分:

 proxy: {
    headers: { 'Accept': 'application/json' },
    limitParam: undefined,
    pageParam: undefined,
    startParam: undefined,
    paramsAsJson: false,
    type: 'ajax',        
    // ...
    reader: {
        type: 'json',
        rootProperty: 'Data',
        messageProperty: 'Message'
    },
    // ...
},

下面是从服务器返回的一些数据(省略了数组内的数据:

{
 "Data":[
 ],
 "Message":"Success!"
}

该应用程序似乎在读取数据属性时没有问题(即我的网格工作正常),但我无法访问同步方法的回调或成功事件中的消息属性。这是错误消息:

Uncaught TypeError: Cannot read property 'Message' of undefined(…)
createAccessor: (function () {
    var re = /['['.]/;
    return function (expr) {
        var me = this,
            simple = me.getUseSimpleAccessors(),
            operatorIndex, result, current, parts, part, inExpr, isDot, isLeft, isRight, special, c, i, bracketed, len;
        if (!(expr || expr === 0)) {
            return;
        }
        if (typeof expr === 'function') {
            return expr;
        }
        if (!simple) {
            operatorIndex = String(expr).search(re);
        }
        if (simple === true || operatorIndex < 0) {
            result = function (raw) {
                return raw[expr];  <-------- This is where it goes wrong at some point
            };
        } 

如果我通过此代码进行调试,则在某些时候原始变量会丢失其值,从而给出预期的未定义错误消息。我的问题是我如何能够在 Ext 6 存储的回调或成功函数中检索消息属性?

在 ExtJS 6 中,Sencha 已将默认值 keepRawData 更改为 false。如果将其更改回 true,则一切应再次按预期工作。Sencha文档告诉我们存在内存泄漏的可能性,但我还没有遇到过这样的问题。

您可能不必更改默认的 keepRawData 值在 ExtJS 6 中,您还可以使用以下方法从操作参数访问 messageProperty:

operation.error;

operation.getError();

唯一的问题是,如果成功参数为 true,则未设置 error 属性。在这种情况下,我想他们希望您使用 records 参数,因为无论如何,这是来自服务器的数据的位置。