Skip to content

Commit 35b98fb

Browse files
author
ehynds
committed
make sure events bound to the document are later unbound. fixes #248
1 parent 3195cf0 commit 35b98fb

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/jquery.multiselect.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
(function($, undefined) {
2222

2323
var multiselectID = 0;
24+
var $doc = $(document);
2425

2526
$.widget("ech.multiselect", {
2627

@@ -49,6 +50,11 @@
4950
this.speed = $.fx.speeds._default; // default speed for effects
5051
this._isOpen = false; // assume no
5152

53+
// create a unique namespace for events that the widget
54+
// factory cannot unbind automatically. Use eventNamespace if on
55+
// jQuery UI 1.9+, and otherwise fallback to a custom string.
56+
this._namespaceID = this.eventNamespace || ('multiselect' + multiselectID);
57+
5258
var button = (this.button = $('<button type="button"><span class="ui-icon ui-icon-triangle-2-n-s"></span></button>'))
5359
.addClass('ui-multiselect ui-widget ui-state-default ui-corner-all')
5460
.addClass(o.classes)
@@ -96,6 +102,9 @@
96102
if(!o.multiple) {
97103
menu.addClass('ui-multiselect-single');
98104
}
105+
106+
// bump unique ID
107+
multiselectID++;
99108
},
100109

101110
_init: function() {
@@ -120,7 +129,7 @@
120129
var checkboxContainer = this.checkboxContainer;
121130
var optgroups = [];
122131
var html = "";
123-
var id = el.attr('id') || multiselectID++; // unique ID for the label & option tags
132+
var id = el.attr('id') || multiselectID; // unique ID for the label & option tags
124133

125134
// build items
126135
el.find('option').each(function(i) {
@@ -384,7 +393,7 @@
384393
});
385394

386395
// close each widget when clicking on any other element/anywhere else on the page
387-
$(document).bind('mousedown.multiselect', function(e) {
396+
$doc.bind('mousedown.' + this._namespaceID, function(e) {
388397
if(self._isOpen && !$.contains(self.menu[0], e.target) && !$.contains(self.button[0], e.target) && e.target !== self.button[0]) {
389398
self.close();
390399
}
@@ -619,6 +628,9 @@
619628
// remove classes + data
620629
$.Widget.prototype.destroy.call(this);
621630

631+
// unbind events
632+
$doc.unbind(this._namespaceID);
633+
622634
this.button.remove();
623635
this.menu.remove();
624636
this.element.show();

0 commit comments

Comments
 (0)