1
0
mirror of synced 2025-02-11 01:29:23 +03:00

Merge pull request #1474 from geophree/query-fix

Ignore old queries, remove highlight when beginning a new query.
This commit is contained in:
Igor Vaynberg 2013-07-03 15:19:15 -07:00
commit ea14b7c30d

View File

@ -408,7 +408,6 @@ the specific language governing permissions and limitations under the Apache Lic
*/ */
function ajax(options) { function ajax(options) {
var timeout, // current scheduled but not yet executed request var timeout, // current scheduled but not yet executed request
requestSequence = 0, // sequence used to drop out-of-order responses
handler = null, handler = null,
quietMillis = options.quietMillis || 100, quietMillis = options.quietMillis || 100,
ajaxUrl = options.url, ajaxUrl = options.url,
@ -417,9 +416,7 @@ the specific language governing permissions and limitations under the Apache Lic
return function (query) { return function (query) {
window.clearTimeout(timeout); window.clearTimeout(timeout);
timeout = window.setTimeout(function () { timeout = window.setTimeout(function () {
requestSequence += 1; // increment the sequence var data = options.data, // ajax data function
var requestNumber = requestSequence, // this request's sequence number
data = options.data, // ajax data function
url = ajaxUrl, // ajax url string or function url = ajaxUrl, // ajax url string or function
transport = options.transport || $.fn.select2.ajaxDefaults.transport, transport = options.transport || $.fn.select2.ajaxDefaults.transport,
// deprecated - to be removed in 4.0 - use params instead // deprecated - to be removed in 4.0 - use params instead
@ -449,9 +446,6 @@ the specific language governing permissions and limitations under the Apache Lic
dataType: options.dataType, dataType: options.dataType,
data: data, data: data,
success: function (data) { success: function (data) {
if (requestNumber < requestSequence) {
return;
}
// TODO - replace query.page with query so users have access to term, page, etc. // TODO - replace query.page with query so users have access to term, page, etc.
var results = options.results(data, query.page); var results = options.results(data, query.page);
query.callback(results); query.callback(results);
@ -703,6 +697,7 @@ the specific language governing permissions and limitations under the Apache Lic
this.results = results = this.container.find(resultsSelector); this.results = results = this.container.find(resultsSelector);
this.search = search = this.container.find("input.select2-input"); this.search = search = this.container.find("input.select2-input");
this.queryCount = 0;
this.resultsPage = 0; this.resultsPage = 0;
this.context = null; this.context = null;
@ -1428,7 +1423,7 @@ the specific language governing permissions and limitations under the Apache Lic
if (index >= choices.length) index = choices.length - 1; if (index >= choices.length) index = choices.length - 1;
if (index < 0) index = 0; if (index < 0) index = 0;
this.results.find(".select2-highlighted").removeClass("select2-highlighted"); this.removeHighlight();
choice = $(choices[index]); choice = $(choices[index]);
choice.addClass("select2-highlighted"); choice.addClass("select2-highlighted");
@ -1441,6 +1436,10 @@ the specific language governing permissions and limitations under the Apache Lic
} }
}, },
removeHighlight: function() {
this.results.find(".select2-highlighted").removeClass("select2-highlighted");
},
// abstract // abstract
countSelectableResults: function() { countSelectableResults: function() {
return this.findHighlightableChoices().length; return this.findHighlightableChoices().length;
@ -1453,8 +1452,8 @@ the specific language governing permissions and limitations under the Apache Lic
var choices = this.findHighlightableChoices(); var choices = this.findHighlightableChoices();
this.highlight(choices.index(el)); this.highlight(choices.index(el));
} else if (el.length == 0) { } else if (el.length == 0) {
// if we are over an unselectable item remove al highlights // if we are over an unselectable item remove all highlights
this.results.find(".select2-highlighted").removeClass("select2-highlighted"); this.removeHighlight();
} }
}, },
@ -1521,7 +1520,9 @@ the specific language governing permissions and limitations under the Apache Lic
self = this, self = this,
input, input,
term = search.val(), term = search.val(),
lastTerm=$.data(this.container, "select2-last-term"); lastTerm = $.data(this.container, "select2-last-term"),
// sequence number used to drop out-of-order responses
queryNumber;
// prevent duplicate queries against the same term // prevent duplicate queries against the same term
if (initial !== true && lastTerm && equal(term, lastTerm)) return; if (initial !== true && lastTerm && equal(term, lastTerm)) return;
@ -1543,6 +1544,8 @@ the specific language governing permissions and limitations under the Apache Lic
postRender(); postRender();
} }
queryNumber = ++this.queryCount;
var maxSelSize = this.getMaximumSelectionSize(); var maxSelSize = this.getMaximumSelectionSize();
if (maxSelSize >=1) { if (maxSelSize >=1) {
data = this.data(); data = this.data();
@ -1577,6 +1580,8 @@ the specific language governing permissions and limitations under the Apache Lic
search.addClass("select2-active"); search.addClass("select2-active");
this.removeHighlight();
// give the tokenizer a chance to pre-process the input // give the tokenizer a chance to pre-process the input
input = this.tokenize(); input = this.tokenize();
if (input != undefined && input != null) { if (input != undefined && input != null) {
@ -1594,6 +1599,11 @@ the specific language governing permissions and limitations under the Apache Lic
callback: this.bind(function (data) { callback: this.bind(function (data) {
var def; // default choice 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 // ignore a response if the select2 has been closed before it was received
if (!this.opened()) { if (!this.opened()) {
this.search.removeClass("select2-active"); this.search.removeClass("select2-active");