ok( !event.isDefaultPrevented(), "default action is prevented" );
});
-})(jQuery);
+asyncTest( "handle race condition", function() {
+ expect( 3 );
+ var count = 0,
+ element = $( "#autocomplete" ).autocomplete({
+ source: function( request, response ) {
+ count++;
+ if ( request.term.length === 1 ) {
+ equal( count, 1, "request with 1 character is first" );
+ setTimeout(function() {
+ response([ "one" ]);
+ setTimeout( checkResults, 1 );
+ }, 1 );
+ return;
+ }
+ equal( count, 2, "request with 2 characters is second" );
+ response([ "two" ]);
+ }
+ });
+
+ element.autocomplete( "search", "a" );
+ element.autocomplete( "search", "ab" );
+
+ function checkResults() {
+ equal( element.autocomplete( "widget" ).find( ".ui-menu-item" ).text(), "two",
+ "correct results displayed" );
+ start();
+ }
+});
+
+}( jQuery ) );
}, 150 );
});
this._initSource();
- this.response = function() {
- return self._response.apply( self, arguments );
- };
this.menu = $( "<ul></ul>" )
.addClass( "ui-autocomplete" )
.appendTo( $( this.options.appendTo || "body", doc )[0] )
url: url,
data: request,
dataType: "json",
- context: {
- autocompleteRequest: ++requestIndex
- },
success: function( data, status ) {
- if ( this.autocompleteRequest === requestIndex ) {
- response( data );
- }
+ response( data );
},
error: function() {
- if ( this.autocompleteRequest === requestIndex ) {
- response( [] );
- }
+ response( [] );
}
});
};
this.pending++;
this.element.addClass( "ui-autocomplete-loading" );
- this.source( { term: value }, this.response );
+ this.source( { term: value }, this._response() );
+ },
+
+ _response: function() {
+ var that = this,
+ index = ++requestIndex;
+
+ return function( content ) {
+ if ( index === requestIndex ) {
+ that.__response( content );
+ }
+
+ that.pending--;
+ if ( !that.pending ) {
+ that.element.removeClass( "ui-autocomplete-loading" );
+ }
+ };
},
- _response: function( content ) {
+ __response: function( content ) {
if ( !this.options.disabled && content && content.length ) {
content = this._normalize( content );
this._suggest( content );
} else {
this.close();
}
- this.pending--;
- if ( !this.pending ) {
- this.element.removeClass( "ui-autocomplete-loading" );
- }
},
close: function( event ) {