File tree Expand file tree Collapse file tree 2 files changed +46
-4
lines changed
Expand file tree Collapse file tree 2 files changed +46
-4
lines changed Original file line number Diff line number Diff line change @@ -385,4 +385,40 @@ describe('Autocomplete', function () {
385385
386386 expect ( instance instanceof $ . Autocomplete ) . toBe ( true ) ;
387387 } ) ;
388+
389+ it ( 'Should construct serviceUrl via callback function.' , function ( ) {
390+ var input = $ ( document . createElement ( 'input' ) ) ,
391+ dynamicUrl ,
392+ data ;
393+
394+ input . autocomplete ( {
395+ ignoreParams : true ,
396+ serviceUrl : function ( query ) {
397+ return '/dynamic-url/' + encodeURIComponent ( query ) . replace ( / % 2 0 / g, "+" ) ;
398+ }
399+ } ) ;
400+
401+ $ . mockjax ( {
402+ url : '/dynamic-url/*' ,
403+ responseTime : 5 ,
404+ response : function ( settings ) {
405+ dynamicUrl = settings . url ;
406+ data = settings . data ;
407+ var response = {
408+ suggestions : [ ]
409+ } ;
410+ this . responseText = JSON . stringify ( response ) ;
411+ }
412+ } ) ;
413+
414+ input . val ( 'Hello World' ) ;
415+ input . autocomplete ( ) . onValueChange ( ) ;
416+
417+ waits ( 10 ) ;
418+
419+ runs ( function ( ) {
420+ expect ( dynamicUrl ) . toBe ( '/dynamic-url/Hello+World' ) ;
421+ expect ( data ) . toBeFalsy ( ) ;
422+ } ) ;
423+ } ) ;
388424} ) ;
Original file line number Diff line number Diff line change 381381 getSuggestions : function ( q ) {
382382 var response ,
383383 that = this ,
384- options = that . options ;
384+ options = that . options ,
385+ serviceUrl = options . serviceUrl ;
385386
386387 response = that . isLocal ? that . getSuggestionsLocal ( q ) : that . cachedResponse [ q ] ;
387388
390391 that . suggest ( ) ;
391392 } else if ( ! that . isBadQuery ( q ) ) {
392393 options . params [ options . paramName ] = q ;
393- options . onSearchStart . call ( that . element , options . params ) ;
394+ if ( options . onSearchStart . call ( that . element , options . params ) === false ) {
395+ return ;
396+ }
397+ if ( $ . isFunction ( options . serviceUrl ) ) {
398+ serviceUrl = options . serviceUrl . call ( that . element , q ) ;
399+ }
394400 $ . ajax ( {
395- url : options . serviceUrl ,
396- data : options . params ,
401+ url : serviceUrl ,
402+ data : options . ignoreParams ? null : options . params ,
397403 type : options . type ,
398404 dataType : options . dataType
399405 } ) . done ( function ( data ) {
You can’t perform that action at this time.
0 commit comments