From: Scott González Date: Thu, 25 Apr 2013 14:59:20 +0000 (-0400) Subject: Autocomplete: Refactored _response() so that requestIndex is defined closer to where... X-Git-Tag: 1.10.4~95 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3de683d135af4480a62911f4b48d287718a2fa05;p=jquery-ui.git Autocomplete: Refactored _response() so that requestIndex is defined closer to where it's used. (cherry picked from commit 947d738c0b9403da527b31ad2af3b6b9150383bd) --- diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index 789f736ce..5dea71287 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -16,9 +16,6 @@ */ (function( $, undefined ) { -// used to prevent race conditions with remote data sources -var requestIndex = 0; - $.widget( "ui.autocomplete", { version: "@VERSION", defaultElement: "", @@ -418,21 +415,24 @@ $.widget( "ui.autocomplete", { this.source( { term: value }, this._response() ); }, - _response: function() { - var that = this, - index = ++requestIndex; + _response: (function() { + var requestIndex = 0; - return function( content ) { - if ( index === requestIndex ) { - that.__response( content ); - } + return function() { + var index = ++requestIndex; - that.pending--; - if ( !that.pending ) { - that.element.removeClass( "ui-autocomplete-loading" ); - } + return $.proxy(function( content ) { + if ( index === requestIndex ) { + this.__response( content ); + } + + this.pending--; + if ( !this.pending ) { + this.element.removeClass( "ui-autocomplete-loading" ); + } + }, this ); }; - }, + })(), __response: function( content ) { if ( content ) {