1

I have a store which I'd like to sync() with server.

Store.sync() method have success and failure property-functions, they have Ext.data.Batch and options as parameters.

If I get such response from server:

{
  success: false,
  msg: "test error!"
}

failure method invokes.

How to access response msg property from within failure method?

s.webbandit
  • 16,332
  • 16
  • 58
  • 82

2 Answers2

6
    store.sync({
        failure: function(batch, options) {
            alert(batch.proxy.getReader().jsonData.msg);
        }
    });
Vlad
  • 3,626
  • 16
  • 14
  • `jsonData` reader's property is deprecated **deprecated Will be removed in Ext JS 5.0. This is just a copy of this.rawData - use that instead.** Indeed they removed the `jsonData` in 5th version. – babinik Jul 03 '14 at 21:42
  • 1
    @Vlad `batch.proxy.getReader().jsonData` contains the response for the last operation in the batch. Having proxy with default configuration `batchActions = true` and `batchOrder = 'create,update,destroy'` and batch (`Ext.data.Batch` default configuration `pauseOnException = false`) with create and update operations, if create operation failed but the update operation succeeded, then the reader's `rawData` would have the success message. – babinik Jul 03 '14 at 22:34
2

Store.sync() method have success and failure property-functions, they have Ext.data.Batch and options as parameters.

Any failed operations collecting inside batch.exceptions array. It has all what you need. Just go through the failed operations, processing them.

Operation failure message ("test error!") would be inside - operation.error

store.sync({
    failure: function(batch, options) {
        Ext.each(batch.exceptions, function(operation) {
            if (operation.hasException()) {
                Ext.log.warn('error message: ' + operation.error);
            }
        });
    }
});

Proxy set it inside processResponse method:

operation.setException(result.message);

Where:

  • Operation's setException method sets its error property (error message there).
  • result is Ext.data.ResultSet and the result.message if filled by the proxy's reader, using the reader's messageProperty

But, by default reader's message property is messageProperty: 'message'. In your case you should configured the reader with correct messageProperty, like:

reader: {
    ...
    messageProperty: 'msg'
}

or return from the server response with metadata configuration property, like:

{
    "success": false,
    "msg": "test error!", 
    "metaData": {
        ...
        "messageProperty": 'msg'
    }
}

Json reader - Response MetaData (docs section)

babinik
  • 646
  • 3
  • 10