Is there a shorter/cleaner way to do the null/undefined testing?
Most of these solutions do not work in a certain case for me, where I am setting an attribute:
...
The template and with bindings would not work here, because if I did not have an active descendant, then my div would be empty. For my solution, I created an observable method:
ko.observable.fn.get = function (propertyName, defaultValue) {
var
self = this(),
propertyValue;
if (self != null) {
propertyValue = ko.unwrap(self[propertyName]);
}
return propertyValue || defaultValue;
}
Which allows me to change my binding to this:
...