Last active
August 29, 2015 14:06
-
-
Save Craga89/ce3da86df793b677b0c2 to your computer and use it in GitHub Desktop.
Knockout + Bootstrap 3 - Buttons and the `enable` binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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