views:

27

answers:

1

I found this a common issue, but it seems that there is no obvious solution after googling.

In my page, some user action would trigger AJAX request. With YUI 2.X, the code is like below:

Connect.asyncRequest("POST", url, 
   {
      'failure' : function() { alert('failed'); },
      'success': function() { doSuccess(); },
      'scope": this,
   },
   dataStr);

When the AJAX request fails, i'd like to pop up a dialog (not necessarily alert) to show the failure. However, if the AJAX request takes some seconds, and I hit "Refresh" button to reload the page, the dialog is always pops up. This is not good. So I want to surpress the dialog when reloading.

When the page is reloaded, the "failure" callback will be invoked with object {status: 0, statusText: 'communication failure'}. This doesn't distinguish itself with other kind of connection failure. So, I cannot judge whether it is unloading phase based on the callback object.

Currently, my workaround is to listen on "beforeunload" event.

Event.on(window, 'beforeunload', function() { isUnloading = true; });

The failure callback check whether to show dialog based on the isUnloading value.

This works, but the "beforeunload" event is not standard. Is there any better way to handle such case?

Thanks

A: 

From tye YUI 2 API, it looks like you should be able to:

var connection = Connect.asyncRequest("POST", url, 
   {
      'failure' : function() { alert('failed'); },
      'success': function() { doSuccess(); },
      'scope": this,
   },
   dataStr);
Event.on(window, 'beforeunload', function() { Connect.abort(connection, function() {...}, false ); });

I have not used YUI very much (I primarily use jquery), but you could give this a try.

RMorrisey