Created
December 23, 2011 18:56
-
-
Save ssayala/1515068 to your computer and use it in GitHub Desktop.
knockout binding to disable and clear an element with value 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
ko.bindingHandlers.disableClear = { | |
update: function (element, valueAccessor, allBindingsAccessor) { | |
var value = !ko.utils.unwrapObservable(valueAccessor()); | |
if (value && element.disabled) | |
element.removeAttribute("disabled"); | |
else if ((!value) && (!element.disabled)) { | |
//If there is a value binding, that's what we want to clear | |
var valueBinding = allBindingsAccessor().value; | |
if ( typeof(valueBinding) === 'function') { | |
valueBinding(''); | |
} | |
element.disabled = true; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment