View Full Version : AJAX global handlers question

20 Nov 2010, 7:24 AM
Hi everyone!

Quick question: Overriding an AJAX global handler like "requestexception" with, suppose, a submit "failure" handler. What does ExtJS do? it calls both listeners or overrides the requestexception with the "failure" handler?

What I'd want to do is put an AJAX global handler with a window with a generic message for each possible failure (SERVER_INVALID, CLIENT_INVALID, etc). BUT.. I'd want to handle the failure in other special cases in a different way, so I'd need to disable the global handler and use the specific handler.

And I'd need the other way around with, let's say, a login window. I'll use a global "requestexception" listener to check for a 401 HTTP code and, in that case, show the login window. But in that case I don't want to execute other specific listeners that would be fired if the user would be still logged. So I'd need to disable all listeners and just show the login window.

Is this possible?


20 Nov 2010, 8:52 PM
Let's say you have a "Main.js" file for bootstrapping everything in your application. You could setup top level, globally accessible methods like this:


yourcompany.Main = function () {
return {
// Convenience wrapper method.
asyncRequest: function (url, callback, failure, scope, params, timeout) {
var t = (typeof (timeout) == 'number') ? timeout : 1 * 30 * 1000;

url: url,
params: typeof params == 'object' ? params : null,
success: callback,
failure: typeof failure == 'function' ? failure : yourcompany.Main.asyncFailure,
scope: scope,
timeout: t

// Global error handler.
asyncFailure: function (response, options) {
var response = JSON.decode(response.responseText);

title: 'Request Error',
msg: response.error.message,
buttons: Ext.MessageBox.OK,
icon: Ext.MessageBox.ERROR,
minWidth: 350

If you need to use a custom error handler, pass it in to the "failure" param of the asyncRequest() method. If not, it will use the globally defined and shared one.

21 Nov 2010, 7:49 AM
That's a good way to handle requests that I made in my code. But the problem still remains in requests for which I don't have control, like the one made by a submit from a form. I could create a method that handle globally the exception and then:

Ext.getCmp( 'myForm' ).getForm().submit( {
failure: myGlobalExceptionHandler
} );

But, that way I'd need to configure every component with my global handler. What I was thinking about is on configure the global requestexception event with my global exception handler:

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

And then only configure the 'failure' option when I need to do something specific that the global handler doesn't cover.

Is this possible? and if I put a listener to the global 'requestexception' AJAX event and then put a handler when a submit fails on a form. What happens? both listeners get fired? can I disable in a specific case the global 'requestexception' listener from my specific handler?

Thanks a lot!

22 Nov 2010, 2:01 AM
The requestexception event gets fired before the failure handler, so you can't disable the event from the handler (but you can disable the handler from the event).

You could add some flag to the request options that the event handler can check on.

22 Nov 2010, 7:35 AM
Yes, that should do the trick. I'll give it a try.

Thanks a lot!