Skip to content

Commit

Permalink
Fix selectOnBlur and closeOnSelect combination
Browse files Browse the repository at this point in the history
This fixes an infinite loop that used to be caused when both
`closeOnSelect` and `selectOnClose` used to be combined, because they
both were listening to events triggered by the other one. The problem
was that `selectOnClose` was triggering `select` events for data objects
which had already been selected. This problem was solved by checking if
the data object was already selected before trying to select it again.

This closes select2#3751.
This closes select2#3169.
  • Loading branch information
kevin-brown committed Nov 27, 2015
1 parent d504700 commit 393ca4c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/js/select2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ define([
'select': 'selecting',
'unselect': 'unselecting'
};

if (args === undefined) {
args = {};
}
Expand Down
13 changes: 12 additions & 1 deletion src/js/select2/dropdown/selectOnClose.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ define([
SelectOnClose.prototype._handleSelectOnClose = function () {
var $highlightedResults = this.getHighlightedResults();

// Only select highlighted results
if ($highlightedResults.length < 1) {
return;
}

var data = $highlightedResults.data('data');

// Don't re-select already selected resulte
if (
(data.element != null && data.element.selected) ||
(data.element == null && data.selected)
) {
return;
}

this.trigger('select', {
data: $highlightedResults.data('data')
data: data
});
};

Expand Down

0 comments on commit 393ca4c

Please sign in to comment.