Skip to content

Commit 1eb461a

Browse files
committed
Ignore old queries, remove highlight when beginning a new query.
1 parent 76f9bae commit 1eb461a

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

select2.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ the specific language governing permissions and limitations under the Apache Lic
687687
this.results = results = this.container.find(resultsSelector);
688688
this.search = search = this.container.find("input.select2-input");
689689

690+
this.queryCount = 0;
690691
this.resultsPage = 0;
691692
this.context = null;
692693

@@ -1412,7 +1413,7 @@ the specific language governing permissions and limitations under the Apache Lic
14121413
if (index >= choices.length) index = choices.length - 1;
14131414
if (index < 0) index = 0;
14141415

1415-
this.results.find(".select2-highlighted").removeClass("select2-highlighted");
1416+
this.removeHighlight();
14161417

14171418
choice = $(choices[index]);
14181419
choice.addClass("select2-highlighted");
@@ -1425,6 +1426,10 @@ the specific language governing permissions and limitations under the Apache Lic
14251426
}
14261427
},
14271428

1429+
removeHighlight: function() {
1430+
this.results.find(".select2-highlighted").removeClass("select2-highlighted");
1431+
},
1432+
14281433
// abstract
14291434
countSelectableResults: function() {
14301435
return this.findHighlightableChoices().length;
@@ -1437,8 +1442,8 @@ the specific language governing permissions and limitations under the Apache Lic
14371442
var choices = this.findHighlightableChoices();
14381443
this.highlight(choices.index(el));
14391444
} else if (el.length == 0) {
1440-
// if we are over an unselectable item remove al highlights
1441-
this.results.find(".select2-highlighted").removeClass("select2-highlighted");
1445+
// if we are over an unselectable item remove all highlights
1446+
this.removeHighlight();
14421447
}
14431448
},
14441449

@@ -1505,7 +1510,8 @@ the specific language governing permissions and limitations under the Apache Lic
15051510
self = this,
15061511
input,
15071512
term = search.val(),
1508-
lastTerm=$.data(this.container, "select2-last-term");
1513+
lastTerm = $.data(this.container, "select2-last-term"),
1514+
queryNumber;
15091515

15101516
// prevent duplicate queries against the same term
15111517
if (initial !== true && lastTerm && equal(term, lastTerm)) return;
@@ -1527,6 +1533,8 @@ the specific language governing permissions and limitations under the Apache Lic
15271533
postRender();
15281534
}
15291535

1536+
queryNumber = ++this.queryCount;
1537+
15301538
var maxSelSize = this.getMaximumSelectionSize();
15311539
if (maxSelSize >=1) {
15321540
data = this.data();
@@ -1561,6 +1569,8 @@ the specific language governing permissions and limitations under the Apache Lic
15611569

15621570
search.addClass("select2-active");
15631571

1572+
this.removeHighlight();
1573+
15641574
// give the tokenizer a chance to pre-process the input
15651575
input = this.tokenize();
15661576
if (input != undefined && input != null) {
@@ -1578,6 +1588,11 @@ the specific language governing permissions and limitations under the Apache Lic
15781588
callback: this.bind(function (data) {
15791589
var def; // default choice
15801590

1591+
// ignore old responses
1592+
if (queryNumber != this.queryCount) {
1593+
return;
1594+
}
1595+
15811596
// ignore a response if the select2 has been closed before it was received
15821597
if (!this.opened()) {
15831598
this.search.removeClass("select2-active");

0 commit comments

Comments
 (0)