Skip to content

Commit 85593de

Browse files
committed
Fixed bug when destroying Select2
When destroying Select2, an error could be raised while destroying the selection container. This was because the destroy handler was relying on the container id being present. The old functionality has been restored where Select2 will destroy itself when it is initialized again.
1 parent ade7180 commit 85593de

9 files changed

Lines changed: 25 additions & 14 deletions

File tree

dist/js/select2.amd.full.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ define('select2/selection/base',[
660660
var id = container.id + '-container';
661661
var resultsId = container.id + '-results';
662662

663+
this.container = container;
664+
663665
this.$selection.attr('aria-owns', resultsId);
664666

665667
this.$selection.on('keydown', function (evt) {
@@ -716,7 +718,7 @@ define('select2/selection/base',[
716718

717719
BaseSelection.prototype.destroy = function () {
718720
// Unbind the dropdown click handler if it exists
719-
$(document.body).off('.select2.' + container.id);
721+
$(document.body).off('.select2.' + this.container.id);
720722
};
721723

722724
BaseSelection.prototype.update = function (data) {
@@ -3101,7 +3103,7 @@ define('select2/core',[
31013103
], function ($, Options, Utils, KEYS) {
31023104
var Select2 = function ($element, options) {
31033105
if ($element.data('select2') != null) {
3104-
return;
3106+
$element.select2('destroy');
31053107
}
31063108

31073109
this.$element = $element;

dist/js/select2.amd.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ define('select2/selection/base',[
660660
var id = container.id + '-container';
661661
var resultsId = container.id + '-results';
662662

663+
this.container = container;
664+
663665
this.$selection.attr('aria-owns', resultsId);
664666

665667
this.$selection.on('keydown', function (evt) {
@@ -716,7 +718,7 @@ define('select2/selection/base',[
716718

717719
BaseSelection.prototype.destroy = function () {
718720
// Unbind the dropdown click handler if it exists
719-
$(document.body).off('.select2.' + container.id);
721+
$(document.body).off('.select2.' + this.container.id);
720722
};
721723

722724
BaseSelection.prototype.update = function (data) {
@@ -3101,7 +3103,7 @@ define('select2/core',[
31013103
], function ($, Options, Utils, KEYS) {
31023104
var Select2 = function ($element, options) {
31033105
if ($element.data('select2') != null) {
3104-
return;
3106+
$element.select2('destroy');
31053107
}
31063108

31073109
this.$element = $element;

dist/js/select2.full.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10195,6 +10195,8 @@ define('select2/selection/base',[
1019510195
var id = container.id + '-container';
1019610196
var resultsId = container.id + '-results';
1019710197

10198+
this.container = container;
10199+
1019810200
this.$selection.attr('aria-owns', resultsId);
1019910201

1020010202
this.$selection.on('keydown', function (evt) {
@@ -10251,7 +10253,7 @@ define('select2/selection/base',[
1025110253

1025210254
BaseSelection.prototype.destroy = function () {
1025310255
// Unbind the dropdown click handler if it exists
10254-
$(document.body).off('.select2.' + container.id);
10256+
$(document.body).off('.select2.' + this.container.id);
1025510257
};
1025610258

1025710259
BaseSelection.prototype.update = function (data) {
@@ -12636,7 +12638,7 @@ define('select2/core',[
1263612638
], function ($, Options, Utils, KEYS) {
1263712639
var Select2 = function ($element, options) {
1263812640
if ($element.data('select2') != null) {
12639-
return;
12641+
$element.select2('destroy');
1264012642
}
1264112643

1264212644
this.$element = $element;

dist/js/select2.full.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js/select2.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,8 @@ define('select2/selection/base',[
10881088
var id = container.id + '-container';
10891089
var resultsId = container.id + '-results';
10901090

1091+
this.container = container;
1092+
10911093
this.$selection.attr('aria-owns', resultsId);
10921094

10931095
this.$selection.on('keydown', function (evt) {
@@ -1144,7 +1146,7 @@ define('select2/selection/base',[
11441146

11451147
BaseSelection.prototype.destroy = function () {
11461148
// Unbind the dropdown click handler if it exists
1147-
$(document.body).off('.select2.' + container.id);
1149+
$(document.body).off('.select2.' + this.container.id);
11481150
};
11491151

11501152
BaseSelection.prototype.update = function (data) {
@@ -3529,7 +3531,7 @@ define('select2/core',[
35293531
], function ($, Options, Utils, KEYS) {
35303532
var Select2 = function ($element, options) {
35313533
if ($element.data('select2') != null) {
3532-
return;
3534+
$element.select2('destroy');
35333535
}
35343536

35353537
this.$element = $element;

dist/js/select2.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"grunt-contrib-uglify": "~0.4.0",
4343
"grunt-contrib-watch": "~0.6.0",
4444
"grunt-gh-pages": "^0.9.1",
45+
"grunt-jekyll": "^0.4.2",
4546
"grunt-sass": "~0.12.0",
4647
"node-sass": "~0.8.6"
4748
}

src/js/select2/core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define([
66
], function ($, Options, Utils, KEYS) {
77
var Select2 = function ($element, options) {
88
if ($element.data('select2') != null) {
9-
return;
9+
$element.select2('destroy');
1010
}
1111

1212
this.$element = $element;

src/js/select2/selection/base.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ define([
2121
var id = container.id + '-container';
2222
var resultsId = container.id + '-results';
2323

24+
this.container = container;
25+
2426
this.$selection.attr('aria-owns', resultsId);
2527

2628
this.$selection.on('keydown', function (evt) {
@@ -77,7 +79,7 @@ define([
7779

7880
BaseSelection.prototype.destroy = function () {
7981
// Unbind the dropdown click handler if it exists
80-
$(document.body).off('.select2.' + container.id);
82+
$(document.body).off('.select2.' + this.container.id);
8183
};
8284

8385
BaseSelection.prototype.update = function (data) {

0 commit comments

Comments
 (0)