Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
🐛 make checkbox respect disabled state. Fix double binding issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcculloh committed Jan 27, 2015
1 parent 08c6cc3 commit 84ce56f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions js/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

// handle events
this.$element.on('change.fu.checkbox', $.proxy( this.itemchecked, this ));
this.$label.unbind('click', $.proxy(this.toggle, this));//unbind previous binds so that double clickage doesn't happen (thus making checkbox appear to not work)
this.$label.on('click', $.proxy(this.toggle, this));//make repeated label clicks work

// set default state
Expand Down Expand Up @@ -78,14 +79,16 @@

enable: function() {
this.state.disabled = false;
this.$element.attr('disabled', false);
this.$element.removeAttr('disabled');
this.$element.prop('disabled', false);
this._resetClasses();
this.$element.trigger( 'enabled.fu.checkbox' );
},

disable: function() {
this.state.disabled = true;
this.$element.attr('disabled', true);
this.$element.prop('disabled', true);
this.$element.attr('disabled', 'disabled');
this._setDisabledClass();
this.$element.trigger( 'disabled.fu.checkbox' );
},
Expand All @@ -111,6 +114,11 @@
},

toggle: function(e) {
//keep checkbox from being used if it is disabled. You can't rely on this.state.disabled, because on bind time it might not be disabled, but, state.disabled may be set to true after bind time (and this.state.disabled won't be updated for this bound instance)
//To see how this works, uncomment the next line of code and go to http://0.0.0.0:8000/index.html click the "disable #myCustomCheckbox1" and then click on the first checkbox and see the disparity in the output between this.state and this.$element.attr
//console.log('is disabled? this.state says, "' + this.state.disabled + '"; this.$element.attr says, "' + this.$element.attr('disabled') + '"');
if(/* do not change this to this.state.disabled. It will break edge cases */ this.$element.prop('disabled')){return;}

//keep event from firing twice in Chrome
if (!e || (e.target === e.originalEvent.target)) {
this.state.checked = !this.state.checked;
Expand Down

0 comments on commit 84ce56f

Please sign in to comment.