From 1eb461a42c698d7e53b5f45a87cbdf291c9ac582 Mon Sep 17 00:00:00 2001 From: Jeff Hanke Date: Thu, 27 Jun 2013 13:08:56 -0700 Subject: [PATCH 1/2] Ignore old queries, remove highlight when beginning a new query. --- select2.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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"); From 2a0cc7623604089f827e9d7f7b8656cc06aa7e65 Mon Sep 17 00:00:00 2001 From: Jeff Hanke Date: Mon, 1 Jul 2013 13:30:44 -0700 Subject: [PATCH 2/2] Remove ajax out-of-order dropping code, handled in updateResults now. --- select2.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/select2.js b/select2.js index 59646e83..7546ae0a 100644 --- a/select2.js +++ b/select2.js @@ -392,7 +392,6 @@ the specific language governing permissions and limitations under the Apache Lic */ function ajax(options) { var timeout, // current scheduled but not yet executed request - requestSequence = 0, // sequence used to drop out-of-order responses handler = null, quietMillis = options.quietMillis || 100, ajaxUrl = options.url, @@ -401,9 +400,7 @@ the specific language governing permissions and limitations under the Apache Lic return function (query) { window.clearTimeout(timeout); timeout = window.setTimeout(function () { - requestSequence += 1; // increment the sequence - var requestNumber = requestSequence, // this request's sequence number - data = options.data, // ajax data function + var data = options.data, // ajax data function url = ajaxUrl, // ajax url string or function transport = options.transport || $.fn.select2.ajaxDefaults.transport, // deprecated - to be removed in 4.0 - use params instead @@ -433,9 +430,6 @@ the specific language governing permissions and limitations under the Apache Lic dataType: options.dataType, data: data, success: function (data) { - if (requestNumber < requestSequence) { - return; - } // TODO - replace query.page with query so users have access to term, page, etc. var results = options.results(data, query.page); query.callback(results); @@ -1511,6 +1505,7 @@ the specific language governing permissions and limitations under the Apache Lic input, term = search.val(), lastTerm = $.data(this.container, "select2-last-term"), + // sequence number used to drop out-of-order responses queryNumber; // prevent duplicate queries against the same term