Skip to content
Permalink
Browse files

Autocomplete: Abort pending ajax requests when disabled and prevent h…

…andling results when disabled. Fixes #6752 - Menu may still render when autocomplete is disabled.
  • Loading branch information...
scottgonzalez committed Dec 17, 2010
1 parent cf48a3c commit 0d0c86210c4a6473888089ddcea918df8c156022
Showing with 5 additions and 2 deletions.
  1. +5 −2 ui/jquery.ui.autocomplete.js
@@ -227,6 +227,9 @@ $.widget( "ui.autocomplete", {
if ( key === "appendTo" ) {
this.menu.element.appendTo( $( value || "body", this.element[0].ownerDocument )[0] )
}
if ( key === "disabled" && value && this.xhr ) {
this.xhr.abort();

This comment has been minimized.

Copy link
@mikehaertl

mikehaertl Apr 23, 2012

@scottgonzalez Sorry, to come back to this old change and not sure, if comments are allowed here. But: I consider this a bad fix, because now you can not use the global jQuery.ajaxError() handler anymore, e.g. to show connection problems to the user. It will always fire an error if you call xhr.abort(). You could build other workarounds. E.g. you could have a stack of xhr objects and for each response only process it, if it is the last xhr request on the stack. This way each request is allowed to happily complete.

This comment has been minimized.

Copy link
@scottgonzalez

scottgonzalez Apr 23, 2012

Author Member

You can easily differentiate aborts and actual errors by checking the status of the request.

This comment has been minimized.

Copy link
@mikehaertl

mikehaertl Apr 23, 2012

Oh, sorry if i missed that, but would the status really be different on request timeout and when aborted?

This comment has been minimized.

Copy link
@scottgonzalez

scottgonzalez Apr 23, 2012

Author Member

Yes. One will have a status of "timeout" and the other will have a status of "abort".

This comment has been minimized.

Copy link
@mikehaertl

mikehaertl Apr 24, 2012

Right, thanks. And sorry for my ignorance but things like these seem not really to be documented - or i just missed it.

This comment has been minimized.

Copy link
@scottgonzalez

scottgonzalez Apr 24, 2012

Author Member

It's documented in the complete and error settings on http://api.jquery.com/jQuery.ajax/

This comment has been minimized.

Copy link
@mikehaertl

mikehaertl Apr 24, 2012

Ok, i don't want to be picky here, but the documentation relates to the statusText argument, not jqXHR.status. The ajaxError handler only receives a jqXHR object. So maybe the docs could be enhanced to make this more clear. I would rather search for this here: http://api.jquery.com/jQuery.ajax/#jqXHR

This comment has been minimized.

Copy link
@jzaefferer

jzaefferer Apr 24, 2012

Member

Could you create an issue for that here? https://github.com/jquery/api.jquery.com/issues

This comment has been minimized.

Copy link
@scottgonzalez

scottgonzalez Apr 24, 2012

Author Member

Actually, as stated in the docs, ajaxError receives much more than just jqXHR:

.ajaxError( handler(event, jqXHR, ajaxSettings, thrownError) )

thrownError will be "abort" if the request is aborted.

This comment has been minimized.

Copy link
@mikehaertl

mikehaertl Apr 24, 2012

But the content of thrownError is again not documented. I'll open an issue for both. Thanks.

}
},

_initSource: function() {
@@ -241,7 +244,7 @@ $.widget( "ui.autocomplete", {
} else if ( typeof this.options.source === "string" ) {
url = this.options.source;
this.source = function( request, response ) {
if (self.xhr) {
if ( self.xhr ) {
self.xhr.abort();
}
self.xhr = $.ajax({
@@ -292,7 +295,7 @@ $.widget( "ui.autocomplete", {
},

_response: function( content ) {
if ( content && content.length ) {
if ( !this.options.disabled && content && content.length ) {
content = this._normalize( content );
this._suggest( content );
this._trigger( "open" );

0 comments on commit 0d0c862

Please sign in to comment.
You can’t perform that action at this time.