|
76 | 76 | dataType: 'text', |
77 | 77 | currentRequest: null, |
78 | 78 | triggerSelectOnValidInput: true, |
| 79 | + preventBadQueries: true, |
79 | 80 | lookupFilter: function (suggestion, originalQuery, queryLowerCase) { |
80 | 81 | return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1; |
81 | 82 | }, |
|
473 | 474 | that = this, |
474 | 475 | options = that.options, |
475 | 476 | serviceUrl = options.serviceUrl, |
476 | | - data, |
| 477 | + params, |
477 | 478 | cacheKey; |
478 | 479 |
|
479 | 480 | options.params[options.paramName] = q; |
480 | | - data = options.ignoreParams ? null : options.params; |
| 481 | + params = options.ignoreParams ? null : options.params; |
481 | 482 |
|
482 | 483 | if (that.isLocal) { |
483 | 484 | response = that.getSuggestionsLocal(q); |
484 | 485 | } else { |
485 | 486 | if ($.isFunction(serviceUrl)) { |
486 | 487 | serviceUrl = serviceUrl.call(that.element, q); |
487 | 488 | } |
488 | | - cacheKey = serviceUrl + '?' + $.param(data || {}); |
| 489 | + cacheKey = serviceUrl + '?' + $.param(params || {}); |
489 | 490 | response = that.cachedResponse[cacheKey]; |
490 | 491 | } |
491 | 492 |
|
|
501 | 502 | } |
502 | 503 | that.currentRequest = $.ajax({ |
503 | 504 | url: serviceUrl, |
504 | | - data: data, |
| 505 | + data: params, |
505 | 506 | type: options.type, |
506 | 507 | dataType: options.dataType |
507 | 508 | }).done(function (data) { |
| 509 | + var result; |
508 | 510 | that.currentRequest = null; |
509 | | - that.processResponse(data, q, cacheKey); |
510 | | - options.onSearchComplete.call(that.element, q); |
| 511 | + result = options.transformResult(data); |
| 512 | + that.processResponse(result, q, cacheKey); |
| 513 | + options.onSearchComplete.call(that.element, q, result.suggestions); |
511 | 514 | }).fail(function (jqXHR, textStatus, errorThrown) { |
512 | 515 | options.onSearchError.call(that.element, q, jqXHR, textStatus, errorThrown); |
513 | 516 | }); |
514 | 517 | } |
515 | 518 | }, |
516 | 519 |
|
517 | 520 | isBadQuery: function (q) { |
| 521 | + if (!this.options.preventBadQueries){ |
| 522 | + return false; |
| 523 | + } |
| 524 | + |
518 | 525 | var badQueries = this.badQueries, |
519 | 526 | i = badQueries.length; |
520 | 527 |
|
|
637 | 644 | return suggestions; |
638 | 645 | }, |
639 | 646 |
|
640 | | - processResponse: function (response, originalQuery, cacheKey) { |
| 647 | + processResponse: function (result, originalQuery, cacheKey) { |
641 | 648 | var that = this, |
642 | | - options = that.options, |
643 | | - result = options.transformResult(response, originalQuery); |
| 649 | + options = that.options; |
644 | 650 |
|
645 | 651 | result.suggestions = that.verifySuggestionsFormat(result.suggestions); |
646 | 652 |
|
647 | 653 | // Cache results if cache is not disabled: |
648 | 654 | if (!options.noCache) { |
649 | 655 | that.cachedResponse[cacheKey] = result; |
650 | | - if (result.suggestions.length === 0) { |
651 | | - that.badQueries.push(cacheKey); |
| 656 | + if (options.preventBadQueries && result.suggestions.length === 0) { |
| 657 | + that.badQueries.push(originalQuery); |
652 | 658 | } |
653 | 659 | } |
654 | 660 |
|
|
0 commit comments