EXTJS 4 - Global exception listener

user798612 picture user798612 · Nov 4, 2011 · Viewed 11.4k times · Source

I have a situation where I am making ajax requests to a server from various Ext gridpanel etc. In an Admin area.

The logged in user will be logged out if there is no activity for eg. 5 minutes which is normal.

In this case the server sends back a redirect 403 to the login page.

Right now I am inserting a:

listeners: {
    exception: function(proxy, response, operation, eOpts) {
        if (response.status == '403')
            window.location = 'login';
    }
}

To every store's proxy which is a little overkill.

Could someone be kind enough and let me know how I could add a listener to all communications between ExtJS and server?

I am using the MVC Application Architecture so it could probably be a one liner in the controller.js or app.js.

Thanks

Answer

Sascha picture Sascha · Nov 5, 2011

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