tags:

views:

13

answers:

1

I have a jQueryUI tab control set up that has two tabs. One loads its content via ajax, and is the default tab. The other is a simple non-ajax tab.

When a user visits the page, the ajax tab is shown. Since it takes at least 5-10 seconds for the content to load, some users click to view the 2nd tab. At this point, it appears like the ajax call is cancelled when the 2nd tab is displayed - the tab's loader disappears, and clicking back to the ajax tab just shows a blank div. Since I have caching turned off, the tab does not reload. Turning on caching would allow the user to at least request the data again, but since these calls can take a loooong time, this is not a workable solution.

The ideal behavior in this situation would be for the ajax tab to continue keep its request open even if other tabs are clicked.

Is this possible?

A: 

The default behavior is to call an .abort() on the pending request, you can see it in the source here:

if ( this.xhr ) {
  this.xhr.abort();
  delete this.xhr;
}

But you could trick it a bit and remove the reference to that previous XmlHeepRequest using the select event handler, like this:

$("#tabs").tabs('destroy').tabs({
  select: function(event, ui) {
    $(this).data("tabs").xhr = null;
  }
});

This works because the select event handler is triggered before that .abort() call when selecting.

Nick Craver
Thanks! I will give this a try and report back.
jmcmichael
OK, it doesn't look like this technique works. The select event is triggered properly, but I have the ajax set as the first (default) tab, and apparently the select event is only fired when the tab is clicked on, and not if it is the default tab. Even so, when I clicked to another tab and back to the ajax tab, it the ajax request was still interrupted when switching to a non-ajax tab. So I'm not sure if this technique would work even if the select event was triggered when an ajax tab loads as the default (without clicking on it).
jmcmichael
I switched the line"$(this).data("tabs").xhr = null;"to"delete this.xhr;"and I believe this works. However, the _cleanup function removes the ajax tab's spinner, which makes it look to the user like the ajax query has been aborted when in reality it has not. I guess this is not ideal, but better than the old behavior. I will see if I can somehow override the behavior of _cleanup to allow the spinner to keep doing its spinny behavior while the ajax request is still pending.
jmcmichael