ExtJS 6 access messageProperty in store sync's callback

孤街醉人 提交于 2021-02-09 07:14:45

问题


I want to display a message from my server in the UI after synchronizing an ExtJS grid. Here's an excerpt of how that goes:

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

Here's a piece of the store definition:

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

Here's some data that comes back from the server (omitted the data inside the array:

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

The application doesn't seem to have issues with reading the data property (i.e. my grid works properly), but I'm not able to access the message property in either the callback or success event of the sync method. This is the error message:

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
            };
        } 

If I debug through this code, at some point the raw variable loses its value, which gives the expected undefined error message. My question is how I could be able to retrieve the message property in the callback or success functions of an Ext 6 store?


回答1:


In ExtJS 6 Sencha has changed the default value of keepRawData to false. If you change it back to true, everything should work as expected again. Sencha docs tell us that there is a chance of a memory leak, but I have not yet experienced such an issue.




回答2:


You may not have to change the default keepRawData value In ExtJS 6, you can also access the messageProperty from the operation parameter using:

operation.error;

or

operation.getError();

only problem is the error property is not set if the success parameter is true. In which case I guess they expect to you to use the records parameter since that is where the data from your server would be anyway.



来源:https://stackoverflow.com/questions/35226480/extjs-6-access-messageproperty-in-store-syncs-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!