2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-09-19 16:59:01 +00:00

Construct serviceUrl via callback function. Fixes #66.

This commit is contained in:
Tomas Kirda 2013-04-24 15:48:05 -05:00
parent 941c8ee4e3
commit d48616adb5
2 changed files with 43 additions and 3 deletions

View File

@ -385,4 +385,40 @@ describe('Autocomplete', function () {
expect(instance instanceof $.Autocomplete).toBe(true);
});
it('Should construct serviceUrl via callback function.', function () {
var input = $(document.createElement('input')),
dynamicUrl,
data;
input.autocomplete({
ignoreParams: true,
serviceUrl: function (query) {
return '/dynamic-url/' + encodeURIComponent(query).replace(/%20/g, "+");
}
});
$.mockjax({
url: '/dynamic-url/*',
responseTime: 5,
response: function (settings) {
dynamicUrl = settings.url;
data = settings.data;
var response = {
suggestions: []
};
this.responseText = JSON.stringify(response);
}
});
input.val('Hello World');
input.autocomplete().onValueChange();
waits(10);
runs(function () {
expect(dynamicUrl).toBe('/dynamic-url/Hello+World');
expect(data).toBeFalsy();
});
});
});

View File

@ -381,7 +381,8 @@
getSuggestions: function (q) {
var response,
that = this,
options = that.options;
options = that.options,
serviceUrl = options.serviceUrl;
response = that.isLocal ? that.getSuggestionsLocal(q) : that.cachedResponse[q];
@ -393,9 +394,12 @@
if (options.onSearchStart.call(that.element, options.params) === false) {
return;
}
if ($.isFunction(options.serviceUrl)) {
serviceUrl = options.serviceUrl.call(that.element, q);
}
$.ajax({
url: options.serviceUrl,
data: options.params,
url: serviceUrl,
data: options.ignoreParams ? null : options.params,
type: options.type,
dataType: options.dataType
}).done(function (data) {