EXTJS 4 - Global exception listener EXTJS 4 - Global exception listener javascript javascript

EXTJS 4 - Global exception listener


In the beginning of your app insert the following snippet. With this EVERY response, whether it's from a store or a form or ..., will be checked and redirect to login page.

Ext.Ajax.on('requestexception', function (conn, response, options) {    if (response.status === 403) {        window.location = 'login';    }});


I'm not really sure if this will catch all ajax requests but assuming you're using AjaxProxy for all communication with the server it should work:handle the 'requestexception' event in the Ext.Ajax singletonsomething like this

Ext.Ajax.on('requestexception', function(conn, response, options, eOpts) {    //your error handling here});

I haven't tried it but if you do, could you post an update here?


A more complete solution, wherein it will be a catch-all is this:

Ext.util.Observable.observe(Ext.data.Connection, {    requestexception: function(conn, response, options) {        if(response.status == '403')            window.location = 'login';    }});

This is because the underlying class, Ext.data.Connection is used not only in Ext.Ajax but as well as the Ext.data.Proxy that is used by Ext.data.Store, Ext.data.Model. This handles exceptions on such calls as store.load() and model.save(). This should be a more complete catch-all handler.

See more details in my blog post.