Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get responsecode from store callback?

Tags:

extjs

extjs4.2

I'm trying to handle session timeout server-side. When getting session timeout, my server sends back a response with json {success: false}, ContentType: 'application/json', ResponseNo: 408

store:

var storeAssets = Ext.create('Ext.data.Store', {
  model : 'modCombo',
  autoLoad : false,
  proxy : { limitParam : undefined,
    startParam : undefined,
    paramName : undefined,
    pageParam : undefined,
    noCache : false,
    type : 'ajax',
    url : '/caricaAssets.json',
    reader : { root : 'data' }
  }
});

And on the client side, I handle callback loading store like this:

storeAssets.load({
  scope: this,
  callback: function(records, operation, success) {
    if (!success) { Ext.Msg.alert('Error'); }
  }
});

To perform different responses, I'd like to change alert. So, if response no. is 408, I can alert session expired (and so on, managing response numbers).

But I didn't find any way to get response no. in store callback!

Any suggestions?

like image 789
Bia1974 Avatar asked Feb 16 '23 11:02

Bia1974


2 Answers

Unfortunately, the callback method does not have the server response passed in as a parameter. This is likely since there are many ways to load data into a store, and not all of them will have a server response.

You can override the proxy's processResponse function to store the server's response with the operation object, then access it in your callback.

Ext.define('Ext.data.proxy.ServerOverride', {
   override: 'Ext.data.proxy.Server',

   processResponse: function (success, operation, request, response, callback, scope) {
      operation.serverResponse = response;
      this.callParent(arguments);
   }
});

Then, to get the status:

storeAssets.load({
   scope: this,
   callback: function(records, operation, success) {
      if (operation.serverResponse.status === 408) {
         Ext.Msg.alert('Session expired');
      }
   }
});
like image 113
Towler Avatar answered Feb 23 '23 20:02

Towler


I know this is quite old now, but I had a similar problem. The solution I found was to listen for the exception event in the proxy.

proxy{
    type: 'ajax',
    reader: {
        type: 'json'
    ,
    listeners: {
        exception: function(proxy, response, options){
            Ext.MessageBox.alert('Error', response.status + ": " + response.statusText); 
        }
    }
}

I also predicated my store load callback to only proceed when success is true. Hopefully someone else searching will find this helpful.

like image 21
Jaimie Whiteside Avatar answered Feb 23 '23 20:02

Jaimie Whiteside