Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXTJS 4 - Global exception listener

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

like image 498
user798612 Avatar asked Nov 04 '11 11:11

user798612


2 Answers

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';     } }); 
like image 107
Sascha Avatar answered Sep 18 '22 12:09

Sascha


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 singleton something 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?

like image 41
nightwatch Avatar answered Sep 18 '22 12:09

nightwatch