If the account
is observable, then you really should use the with
binding like you have it already or, use a computed observable to access the property. Sure, it is a bit verbose, but it must be done.
Using expressions like someObservable().someProperty
will only lead to headaches and confusion and should be avoided. e.g., If you did use this and someProperty
happened to be observable, you may notice that something's not right when someObservable
changes. The binding will not be updated to use the someProperty
of the new value and I hope you can see why.
You can make creating the computed observable in a safe manner easier by creating a function to do so.
ko.observable.fn.property = function (name) {
return ko.computed({
read: function () {
var parentValue = this();
if (parentValue)
return ko.utils.unwrapObservable(parentValue[name]);
},
write: function (value) {
var parentValue = this(), property;
if (parentValue) {
property = parentValue[name];
if (ko.isWriteableObservable(property))
property(value);
}
},
owner: this
});
};
Then you could use this in your bindings:
<span data-bind="text: account.property('shortName')"></span>