Skip to content

Commit 1899996

Browse files
committed
Remove binded events if element is disabled after initialization. Fix #115
1 parent b54051e commit 1899996

File tree

8 files changed

+53
-18
lines changed

8 files changed

+53
-18
lines changed

public/jquery.selectric.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@
136136
* @param {string} f - String to be formated
137137
* @return {string} String formated
138138
*/
139-
format: function (f) {
139+
format: function(f) {
140140
var a = arguments; // store outer arguments
141141
return ('' + f) // force format specifier to String
142142
.replace( // replace tokens in format specifier
143143
/\{(?:(\d+)|(\w+))\}/g, // match {token} references
144-
function (
144+
function(
145145
s, // the matched string (ignored)
146146
i, // an argument index
147147
p // a property name
@@ -266,11 +266,11 @@
266266
outerWrapper : outerWrapper
267267
};
268268

269-
if (_this.options.nativeOnMobile && _this.utils.isMobile()) {
269+
if ( _this.options.nativeOnMobile && _this.utils.isMobile() ) {
270270
_this.elements.input = undefined;
271271
hideSelectWrapper.addClass(_this.classes.prefix + '-is-native');
272272

273-
_this.$element.on('change', function () {
273+
_this.$element.on('change', function() {
274274
_this.refresh();
275275
});
276276
}
@@ -310,6 +310,8 @@
310310
_this.elements.outerWrapper.width(originalWidth);
311311
}
312312

313+
_this.unbindEvents();
314+
313315
if ( !_this.$element.prop('disabled') ) {
314316
_this.state.enabled = true;
315317

@@ -556,15 +558,20 @@
556558
);
557559
},
558560

559-
/** Bind events on the elements */
560-
bindEvents: function() {
561+
/** Remove events on the elements */
562+
unbindEvents: function() {
561563
var _this = this;
562564

563565
_this.elements.wrapper
564566
.add(_this.$element)
565567
.add(_this.elements.outerWrapper)
566568
.add(_this.elements.input)
567569
.off(eventNamespaceSuffix);
570+
},
571+
572+
/** Bind events on the elements */
573+
bindEvents: function() {
574+
var _this = this;
568575

569576
_this.elements.outerWrapper.on('mouseenter' + eventNamespaceSuffix + ' mouseleave' + eventNamespaceSuffix, function(e) {
570577
$(this).toggleClass(_this.classes.hover, e.type === 'mouseenter');

public/jquery.selectric.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.selectric.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@
119119
* @param {string} f - String to be formated
120120
* @return {string} String formated
121121
*/
122-
format: function (f) {
122+
format: function(f) {
123123
var a = arguments; // store outer arguments
124124
return ('' + f) // force format specifier to String
125125
.replace( // replace tokens in format specifier
126126
/\{(?:(\d+)|(\w+))\}/g, // match {token} references
127-
function (
127+
function(
128128
s, // the matched string (ignored)
129129
i, // an argument index
130130
p // a property name
@@ -249,11 +249,11 @@
249249
outerWrapper : outerWrapper
250250
};
251251

252-
if (_this.options.nativeOnMobile && _this.utils.isMobile()) {
252+
if ( _this.options.nativeOnMobile && _this.utils.isMobile() ) {
253253
_this.elements.input = undefined;
254254
hideSelectWrapper.addClass(_this.classes.prefix + '-is-native');
255255

256-
_this.$element.on('change', function () {
256+
_this.$element.on('change', function() {
257257
_this.refresh();
258258
});
259259
}
@@ -293,6 +293,8 @@
293293
_this.elements.outerWrapper.width(originalWidth);
294294
}
295295

296+
_this.unbindEvents();
297+
296298
if ( !_this.$element.prop('disabled') ) {
297299
_this.state.enabled = true;
298300

@@ -539,15 +541,20 @@
539541
);
540542
},
541543

542-
/** Bind events on the elements */
543-
bindEvents: function() {
544+
/** Remove events on the elements */
545+
unbindEvents: function() {
544546
var _this = this;
545547

546548
_this.elements.wrapper
547549
.add(_this.$element)
548550
.add(_this.elements.outerWrapper)
549551
.add(_this.elements.input)
550552
.off(eventNamespaceSuffix);
553+
},
554+
555+
/** Bind events on the elements */
556+
bindEvents: function() {
557+
var _this = this;
551558

552559
_this.elements.outerWrapper.on('mouseenter' + eventNamespaceSuffix + ' mouseleave' + eventNamespaceSuffix, function(e) {
553560
$(this).toggleClass(_this.classes.hover, e.type === 'mouseenter');

test/events.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
/* eslint-env jasmine, jquery */
2+
/* global loadFixtures */
3+
14
'use strict';
25

36
describe('events', function() {
4-
var select;
7+
var select = false;
58

69
beforeEach(function() {
710
jasmine.getFixtures().fixturesPath = 'base/test/fixtures';
@@ -168,4 +171,10 @@ describe('events', function() {
168171
expect(hooks.beforeInit).toHaveBeenCalledTimes(2);
169172

170173
});
174+
175+
it('should not bind events when select is disabled after init', function() {
176+
select.prop('disabled', true).selectric('refresh');
177+
$('.selectric').trigger('click');
178+
expect($('.selectric-wrapper').hasClass('selectric-open')).toBe(false);
179+
});
171180
});

test/keyboard.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
/* eslint-env jasmine, jquery */
2+
/* global loadFixtures, keyvent */
3+
14
'use strict';
25

36
describe('keyboard', function() {
4-
var select;
7+
var select = false;
58

69
beforeEach(function() {
710
jasmine.getFixtures().fixturesPath = 'base/test/fixtures';

test/mobile.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-env jasmine, jquery */
2+
/* global loadFixtures */
3+
14
'use strict';
25

36
describe('mobile', function() {

test/multiple.spec.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
/* eslint-env jasmine, jquery */
2+
/* global loadFixtures */
3+
14
'use strict';
25

36
describe('multiple selects', function() {
4-
var select;
7+
var select = false;
58

69
beforeEach(function() {
710
jasmine.getFixtures().fixturesPath = 'base/test/fixtures';
@@ -77,7 +80,7 @@ describe('multiple selects', function() {
7780
$('.selectric').click();
7881
expect($('.selectric-wrapper').find('.label > h2').length).toBe(1);
7982
});
80-
83+
8184
it('should not select disabled items', function() {
8285
var listItems = $('.selectric-items');
8386
select.find('option').eq(3).prop('disabled', true);

test/visibility.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
/* eslint-env jasmine, jquery */
2+
/* global loadFixtures */
3+
14
'use strict';
25

36
describe('visibility', function() {
4-
var select;
7+
var select = false;
58

69
beforeEach(function() {
710
jasmine.getFixtures().fixturesPath = 'base/test/fixtures';

0 commit comments

Comments
 (0)