diff --git a/select2.js b/select2.js index a0410359..59646e83 100644 --- a/select2.js +++ b/select2.js @@ -687,6 +687,7 @@ the specific language governing permissions and limitations under the Apache Lic this.results = results = this.container.find(resultsSelector); this.search = search = this.container.find("input.select2-input"); + this.queryCount = 0; this.resultsPage = 0; this.context = null; @@ -1412,7 +1413,7 @@ the specific language governing permissions and limitations under the Apache Lic if (index >= choices.length) index = choices.length - 1; if (index < 0) index = 0; - this.results.find(".select2-highlighted").removeClass("select2-highlighted"); + this.removeHighlight(); choice = $(choices[index]); choice.addClass("select2-highlighted"); @@ -1425,6 +1426,10 @@ the specific language governing permissions and limitations under the Apache Lic } }, + removeHighlight: function() { + this.results.find(".select2-highlighted").removeClass("select2-highlighted"); + }, + // abstract countSelectableResults: function() { return this.findHighlightableChoices().length; @@ -1437,8 +1442,8 @@ the specific language governing permissions and limitations under the Apache Lic var choices = this.findHighlightableChoices(); this.highlight(choices.index(el)); } else if (el.length == 0) { - // if we are over an unselectable item remove al highlights - this.results.find(".select2-highlighted").removeClass("select2-highlighted"); + // if we are over an unselectable item remove all highlights + this.removeHighlight(); } }, @@ -1505,7 +1510,8 @@ the specific language governing permissions and limitations under the Apache Lic self = this, input, term = search.val(), - lastTerm=$.data(this.container, "select2-last-term"); + lastTerm = $.data(this.container, "select2-last-term"), + queryNumber; // prevent duplicate queries against the same term if (initial !== true && lastTerm && equal(term, lastTerm)) return; @@ -1527,6 +1533,8 @@ the specific language governing permissions and limitations under the Apache Lic postRender(); } + queryNumber = ++this.queryCount; + var maxSelSize = this.getMaximumSelectionSize(); if (maxSelSize >=1) { data = this.data(); @@ -1561,6 +1569,8 @@ the specific language governing permissions and limitations under the Apache Lic search.addClass("select2-active"); + this.removeHighlight(); + // give the tokenizer a chance to pre-process the input input = this.tokenize(); if (input != undefined && input != null) { @@ -1578,6 +1588,11 @@ the specific language governing permissions and limitations under the Apache Lic callback: this.bind(function (data) { var def; // default choice + // ignore old responses + if (queryNumber != this.queryCount) { + return; + } + // ignore a response if the select2 has been closed before it was received if (!this.opened()) { this.search.removeClass("select2-active");