Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Kirda committed Apr 24, 2013
2 parents 9f62319 + d48616a commit 6e055c1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
36 changes: 36 additions & 0 deletions spec/autocompleteBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
14 changes: 10 additions & 4 deletions src/jquery.autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -390,10 +391,15 @@
that.suggest();
} else if (!that.isBadQuery(q)) {
options.params[options.paramName] = q;
options.onSearchStart.call(that.element, options.params);
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) {
Expand Down

0 comments on commit 6e055c1

Please sign in to comment.