Skip to content

Commit 6e055c1

Browse files
author
Tomas Kirda
committed
Merge branch 'develop'
2 parents 9f62319 + d48616a commit 6e055c1

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

spec/autocompleteBehavior.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff 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(/%20/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
});

src/jquery.autocomplete.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@
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

@@ -390,10 +391,15 @@
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) {

0 commit comments

Comments
 (0)