Skip to content

Commit 0eef299

Browse files
authored
Merge pull request Bttstrp#637 from Bttstrp/remove-push-and-bind
remove push and bind
2 parents 35cc8cb + 758ba92 commit 0eef299

File tree

1 file changed

+20
-29
lines changed

1 file changed

+20
-29
lines changed

src/js/bootstrap-switch.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import jquery from 'jquery';
22

33
const $ = jquery || window.jQuery || window.$;
44

5+
function getClasses(options, id) {
6+
const { state, size, disabled, readonly, indeterminate, inverse } = options;
7+
return [
8+
state ? 'on' : 'off',
9+
size,
10+
disabled ? 'disabled' : undefined,
11+
readonly ? 'readonly' : undefined,
12+
indeterminate ? 'indeterminate' : undefined,
13+
inverse ? 'inverse' : undefined,
14+
id ? `id-${id}` : undefined,
15+
].filter(v => v == null);
16+
}
17+
518
class BootstrapSwitch {
619
constructor(element, options = {}) {
720
this.$element = $(element);
@@ -13,32 +26,10 @@ class BootstrapSwitch {
1326
);
1427
this.prevOptions = {};
1528
this.$wrapper = $('<div>', {
16-
class: () => {
17-
const classes = [];
18-
classes.push(this.options.state ? 'on' : 'off');
19-
if (this.options.size) {
20-
classes.push(this.options.size);
21-
}
22-
if (this.options.disabled) {
23-
classes.push('disabled');
24-
}
25-
if (this.options.readonly) {
26-
classes.push('readonly');
27-
}
28-
if (this.options.indeterminate) {
29-
classes.push('indeterminate');
30-
}
31-
if (this.options.inverse) {
32-
classes.push('inverse');
33-
}
34-
if (this.$element.attr('id')) {
35-
classes.push(`id-${this.$element.attr('id')}`);
36-
}
37-
return classes
38-
.map(this._getClass.bind(this))
39-
.concat([this.options.baseClass], this._getClasses(this.options.wrapperClass))
40-
.join(' ');
41-
},
29+
class: () => getClasses(this.options, this.$element.attr('id'))
30+
.map(v => this._getClass(v))
31+
.concat([this.options.baseClass], this._getClasses(this.options.wrapperClass))
32+
.join(' '),
4233
});
4334
this.$container = $('<div>', { class: this._getClass('container') });
4435
this.$on = $('<span>', {
@@ -54,7 +45,7 @@ class BootstrapSwitch {
5445
class: this._getClass('label'),
5546
});
5647

57-
this.$element.on('init.bootstrapSwitch', this.options.onInit.bind(this, element));
48+
this.$element.on('init.bootstrapSwitch', () => this.options.onInit(element));
5849
this.$element.on('switchChange.bootstrapSwitch', (...args) => {
5950
if (this.options.onSwitchChange.apply(element, args) === false) {
6051
if (this.$element.is(':radio')) {
@@ -402,7 +393,7 @@ class BootstrapSwitch {
402393

403394
_elementHandlers() {
404395
return this.$element.on({
405-
'setPreviousOptions.bootstrapSwitch': this.setPrevOptions.bind(this),
396+
'setPreviousOptions.bootstrapSwitch': () => this.setPrevOptions(),
406397

407398
'previousState.bootstrapSwitch': () => {
408399
this.options = this.prevOptions;
@@ -567,7 +558,7 @@ class BootstrapSwitch {
567558
if (!$.isArray(classes)) {
568559
return [this._getClass(classes)];
569560
}
570-
return classes.map(this._getClass.bind(this));
561+
return classes.map(v => this._getClass(v));
571562
}
572563
}
573564

0 commit comments

Comments
 (0)