Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to http status code from Ext.data.Store

I have an http API which (shockingly new technique) reacts on different errors setting different response statuses.

The question is - while using Ext.data.Store with some XMLHttpRequest-inside proxy, what is the best way to handle this statuses? As far as I can understand, "load" event does not pass status directly, as well as "exception", and the last one actually doesn't even trigger when 4** status is received.

So, as I can see from code xhr instance is hidden from Ext.data.store, so the question is also can be stated as "What is best extjs practice to handle low-level xhr object".

like image 901
shabunc Avatar asked Sep 21 '11 08:09

shabunc


2 Answers

There is no exception event on Ext.data.Store. Instead, it is Ext.data.proxy.Server and its sub-classes (like Ext.data.proxy.Ajax) that define an exception event. Listeners receive a response object which includes the http status.

Depending on your setup, you can register a listener on the store's proxy, or - if your store uses a model - on the model's proxy.

This test setup worked for me on Chrome 14 and FF 6:

var store = Ext.create('Ext.data.Store', {
    fields: [ 'field1', 'field2'],

    proxy: {
        type: 'ajax',
        url: 'api/data.json',
        reader: {
            type: 'json',
            root: 'data'
        },
        listeners: {
            exception: function(proxy, exception, operation) {
                console.log(response.status);
            }

        }
    },
});
store.load(); 
like image 150
mistaecko Avatar answered Oct 10 '22 06:10

mistaecko


The exception event does provide a response object which has a status property containing the HTML status code you want to see.

If your exception is indeed not fired by 4** errors (which in my experience do fire) you could try to register an ajax listener instead:

Ext.Ajax.on('requestexception', exceptionHandlerMethod);

and

function exceptionHandlerMethod(connection, response, requestOptions, listenerOptions) {
    if(response.status == 401) {
        alert('401 error');
    }
}
like image 26
suknic Avatar answered Oct 10 '22 08:10

suknic