From 393ca4cf7f7f7097d3a994bda3dbf195e945eba1 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Fri, 27 Nov 2015 17:32:54 -0500 Subject: [PATCH] Fix selectOnBlur and closeOnSelect combination 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 https://github.com/select2/select2/pull/3751. This closes https://github.com/select2/select2/issues/3169. --- src/js/select2/core.js | 2 +- src/js/select2/dropdown/selectOnClose.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/js/select2/core.js b/src/js/select2/core.js index af48de5e..d42c4ead 100644 --- a/src/js/select2/core.js +++ b/src/js/select2/core.js @@ -357,7 +357,7 @@ define([ 'select': 'selecting', 'unselect': 'unselecting' }; - + if (args === undefined) { args = {}; } diff --git a/src/js/select2/dropdown/selectOnClose.js b/src/js/select2/dropdown/selectOnClose.js index 763d18a2..ee45de10 100644 --- a/src/js/select2/dropdown/selectOnClose.js +++ b/src/js/select2/dropdown/selectOnClose.js @@ -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 }); };