|
21 | 21 | (function($, undefined) {
|
22 | 22 |
|
23 | 23 | var multiselectID = 0;
|
| 24 | + var $doc = $(document); |
24 | 25 |
|
25 | 26 | $.widget("ech.multiselect", {
|
26 | 27 |
|
|
49 | 50 | this.speed = $.fx.speeds._default; // default speed for effects
|
50 | 51 | this._isOpen = false; // assume no
|
51 | 52 |
|
| 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 | + |
52 | 58 | var button = (this.button = $('<button type="button"><span class="ui-icon ui-icon-triangle-2-n-s"></span></button>'))
|
53 | 59 | .addClass('ui-multiselect ui-widget ui-state-default ui-corner-all')
|
54 | 60 | .addClass(o.classes)
|
|
96 | 102 | if(!o.multiple) {
|
97 | 103 | menu.addClass('ui-multiselect-single');
|
98 | 104 | }
|
| 105 | + |
| 106 | + // bump unique ID |
| 107 | + multiselectID++; |
99 | 108 | },
|
100 | 109 |
|
101 | 110 | _init: function() {
|
|
120 | 129 | var checkboxContainer = this.checkboxContainer;
|
121 | 130 | var optgroups = [];
|
122 | 131 | 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 |
124 | 133 |
|
125 | 134 | // build items
|
126 | 135 | el.find('option').each(function(i) {
|
|
384 | 393 | });
|
385 | 394 |
|
386 | 395 | // 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) { |
388 | 397 | if(self._isOpen && !$.contains(self.menu[0], e.target) && !$.contains(self.button[0], e.target) && e.target !== self.button[0]) {
|
389 | 398 | self.close();
|
390 | 399 | }
|
|
619 | 628 | // remove classes + data
|
620 | 629 | $.Widget.prototype.destroy.call(this);
|
621 | 630 |
|
| 631 | + // unbind events |
| 632 | + $doc.unbind(this._namespaceID); |
| 633 | + |
622 | 634 | this.button.remove();
|
623 | 635 | this.menu.remove();
|
624 | 636 | this.element.show();
|
|
0 commit comments