Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Autocomplete: Move race condition logic from ajax requests to general…

… response handler. Fixes #8234 - Autocomplete: Automatic race-condition handling for custom sources.

(cherry picked from commit 96f9c84)

Conflicts:

	tests/unit/autocomplete/autocomplete_core.js
	ui/jquery.ui.autocomplete.js
  • Loading branch information...
commit d040b8f42cc28932deedddebe95473a9fd13d742 1 parent fc6e72b
Scott González authored April 03, 2012
31  tests/unit/autocomplete/autocomplete_core.js
@@ -69,4 +69,33 @@ test( "allow form submit on enter when menu is not active", function() {
69 69
 	ok( !event.isDefaultPrevented(), "default action is prevented" );
70 70
 });
71 71
 
72  
-})(jQuery);
  72
+asyncTest( "handle race condition", function() {
  73
+	expect( 3 );
  74
+	var count = 0,
  75
+		element = $( "#autocomplete" ).autocomplete({
  76
+		source: function( request, response ) {
  77
+			count++;
  78
+			if ( request.term.length === 1 ) {
  79
+				equal( count, 1, "request with 1 character is first" );
  80
+				setTimeout(function() {
  81
+					response([ "one" ]);
  82
+					setTimeout( checkResults, 1 );
  83
+				}, 1 );
  84
+				return;
  85
+			}
  86
+			equal( count, 2, "request with 2 characters is second" );
  87
+			response([ "two" ]);
  88
+		}
  89
+	});
  90
+
  91
+	element.autocomplete( "search", "a" );
  92
+	element.autocomplete( "search", "ab" );
  93
+
  94
+	function checkResults() {
  95
+		equal( element.autocomplete( "widget" ).find( ".ui-menu-item" ).text(), "two",
  96
+			"correct results displayed" );
  97
+		start();
  98
+	}
  99
+});
  100
+
  101
+}( jQuery ) );
38  ui/jquery.ui.autocomplete.js
@@ -131,9 +131,6 @@ $.widget( "ui.autocomplete", {
131 131
 				}, 150 );
132 132
 			});
133 133
 		this._initSource();
134  
-		this.response = function() {
135  
-			return self._response.apply( self, arguments );
136  
-		};
137 134
 		this.menu = $( "<ul></ul>" )
138 135
 			.addClass( "ui-autocomplete" )
139 136
 			.appendTo( $( this.options.appendTo || "body", doc )[0] )
@@ -268,18 +265,11 @@ $.widget( "ui.autocomplete", {
268 265
 					url: url,
269 266
 					data: request,
270 267
 					dataType: "json",
271  
-					context: {
272  
-						autocompleteRequest: ++requestIndex
273  
-					},
274 268
 					success: function( data, status ) {
275  
-						if ( this.autocompleteRequest === requestIndex ) {
276  
-							response( data );
277  
-						}
  269
+						response( data );
278 270
 					},
279 271
 					error: function() {
280  
-						if ( this.autocompleteRequest === requestIndex ) {
281  
-							response( [] );
282  
-						}
  272
+						response( [] );
283 273
 					}
284 274
 				});
285 275
 			};
@@ -310,10 +300,26 @@ $.widget( "ui.autocomplete", {
310 300
 		this.pending++;
311 301
 		this.element.addClass( "ui-autocomplete-loading" );
312 302
 
313  
-		this.source( { term: value }, this.response );
  303
+		this.source( { term: value }, this._response() );
  304
+	},
  305
+
  306
+	_response: function() {
  307
+		var that = this,
  308
+			index = ++requestIndex;
  309
+
  310
+		return function( content ) {
  311
+			if ( index === requestIndex ) {
  312
+				that.__response( content );
  313
+			}
  314
+
  315
+			that.pending--;
  316
+			if ( !that.pending ) {
  317
+				that.element.removeClass( "ui-autocomplete-loading" );
  318
+			}
  319
+		};
314 320
 	},
315 321
 
316  
-	_response: function( content ) {
  322
+	__response: function( content ) {
317 323
 		if ( !this.options.disabled && content && content.length ) {
318 324
 			content = this._normalize( content );
319 325
 			this._suggest( content );
@@ -321,10 +327,6 @@ $.widget( "ui.autocomplete", {
321 327
 		} else {
322 328
 			this.close();
323 329
 		}
324  
-		this.pending--;
325  
-		if ( !this.pending ) {
326  
-			this.element.removeClass( "ui-autocomplete-loading" );
327  
-		}
328 330
 	},
329 331
 
330 332
 	close: function( event ) {

0 notes on commit d040b8f

Please sign in to comment.
Something went wrong with that request. Please try again.