|
93 | 93 | that.selectedIndex = -1;
|
94 | 94 | that.currentValue = that.element.value;
|
95 | 95 | that.intervalId = 0;
|
96 |
| - that.cachedResponse = []; |
| 96 | + that.cachedResponse = {}; |
97 | 97 | that.onChangeInterval = null;
|
98 | 98 | that.onChange = null;
|
99 | 99 | that.isLocal = false;
|
|
220 | 220 | },
|
221 | 221 |
|
222 | 222 | clearCache: function () {
|
223 |
| - this.cachedResponse = []; |
| 223 | + this.cachedResponse = {}; |
224 | 224 | this.badQueries = [];
|
225 | 225 | },
|
226 | 226 |
|
|
463 | 463 | var response,
|
464 | 464 | that = this,
|
465 | 465 | options = that.options,
|
466 |
| - serviceUrl = options.serviceUrl; |
| 466 | + serviceUrl = options.serviceUrl, |
| 467 | + data, |
| 468 | + cacheKey; |
467 | 469 |
|
468 |
| - response = that.isLocal ? that.getSuggestionsLocal(q) : that.cachedResponse[q]; |
| 470 | + options.params[options.paramName] = q; |
| 471 | + data = options.ignoreParams ? null : options.params; |
| 472 | + |
| 473 | + if (that.isLocal) { |
| 474 | + response = that.getSuggestionsLocal(q); |
| 475 | + } else { |
| 476 | + if ($.isFunction(serviceUrl)) { |
| 477 | + serviceUrl = serviceUrl.call(that.element, q); |
| 478 | + } |
| 479 | + cacheKey = serviceUrl + '?' + $.param(data || {}); |
| 480 | + response = that.cachedResponse[cacheKey]; |
| 481 | + } |
469 | 482 |
|
470 | 483 | if (response && $.isArray(response.suggestions)) {
|
471 | 484 | that.suggestions = response.suggestions;
|
472 | 485 | that.suggest();
|
473 | 486 | } else if (!that.isBadQuery(q)) {
|
474 |
| - options.params[options.paramName] = q; |
475 | 487 | if (options.onSearchStart.call(that.element, options.params) === false) {
|
476 | 488 | return;
|
477 | 489 | }
|
478 |
| - if ($.isFunction(options.serviceUrl)) { |
479 |
| - serviceUrl = options.serviceUrl.call(that.element, q); |
480 |
| - } |
481 | 490 | if (that.currentRequest) {
|
482 | 491 | that.currentRequest.abort();
|
483 | 492 | }
|
484 | 493 | that.currentRequest = $.ajax({
|
485 | 494 | url: serviceUrl,
|
486 |
| - data: options.ignoreParams ? null : options.params, |
| 495 | + data: data, |
487 | 496 | type: options.type,
|
488 | 497 | dataType: options.dataType
|
489 | 498 | }).done(function (data) {
|
490 | 499 | that.currentRequest = null;
|
491 |
| - that.processResponse(data, q); |
| 500 | + that.processResponse(data, q, cacheKey); |
492 | 501 | options.onSearchComplete.call(that.element, q);
|
493 | 502 | }).fail(function (jqXHR, textStatus, errorThrown) {
|
494 | 503 | options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown);
|
|
619 | 628 | return suggestions;
|
620 | 629 | },
|
621 | 630 |
|
622 |
| - processResponse: function (response, originalQuery) { |
| 631 | + processResponse: function (response, originalQuery, cacheKey) { |
623 | 632 | var that = this,
|
624 | 633 | options = that.options,
|
625 | 634 | result = options.transformResult(response, originalQuery);
|
|
628 | 637 |
|
629 | 638 | // Cache results if cache is not disabled:
|
630 | 639 | if (!options.noCache) {
|
631 |
| - that.cachedResponse[result[options.paramName]] = result; |
| 640 | + that.cachedResponse[cacheKey] = result; |
632 | 641 | if (result.suggestions.length === 0) {
|
633 |
| - that.badQueries.push(result[options.paramName]); |
| 642 | + that.badQueries.push(cacheKey); |
634 | 643 | }
|
635 | 644 | }
|
636 | 645 |
|
|
0 commit comments