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.11.0-beta.1~345 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=947d738c0b9403da527b31ad2af3b6b9150383bd;p=jquery-ui.git Autocomplete: Refactored _response() so that requestIndex is defined closer to where it's used. --- diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index e79a66a60..5e798d55d 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: "", @@ -416,21 +413,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 ) {