Skip to content

Instantly share code, notes, and snippets.

@Craga89
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save Craga89/ce3da86df793b677b0c2 to your computer and use it in GitHub Desktop.

Select an option

Save Craga89/ce3da86df793b677b0c2 to your computer and use it in GitHub Desktop.
Knockout + Bootstrap 3 - Buttons and the `enable` binding
/**
Enhanced `enable` binding that adds disabled attribute to .btn elements
to support Bootstrap styling.
NOTE: This also effects the `disable` binding handler, since it simply
inverts the `enable` bindings logic internally.
@static
@class Enable
@namespace Base.BindingHandlers
**/
var oldEnable = ko.bindingHandlers.enable,
rIsBtn = /\bbtn\b/;
ko.bindingHandlers.enable = {
update: function (element, valueAccessor) {
var result = oldEnable.update.apply(this, arguments),
enabled = ko.unwrap(valueAccessor());
// Toggle `disabled` class on all `btn` elements
if(rIsBtn.test(element.className)) {
if(enabled) {
element.className.replace(/\bdisabled\b/gi, '');
}
else {
element.className += ' disabled';
}
}
return result;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment