What\'s the suggested \"best practice\" way to use Knockout\'s \"attr\" data binding with standalone attributes like \"readonly\" and
Knockout's "attr" data binding does support this scenario just return null
or undefined
from your getDisabledState()
function then it won't emit the attribute.
Demo Fiddle.
Knockout has an enable binding as well as a disable binding.
I'm not sure if these were available when the question was asked, but anyone referring back to this issue should be aware.
You can also create a binding for readonly like this:
ko.bindingHandlers['readonly'] = {
'update': function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
if (!value && element.readOnly)
element.readOnly = false;
else if (value && !element.readOnly)
element.readOnly = true;
}
};
Source: https://github.com/knockout/knockout/issues/1100