Knockout nested objects and parentheses

前端 未结 2 2076

Few things I don\'t get. According to KO documentation (and many posts here on SO), parentheses should be used when querying and writing observables. But when binding sub-pr

2条回答
  •  时光说笑
    2021-01-12 22:13

    If the binding value is an observable, KO "unwraps" it for you, so you don't have to unwrap it yourself with the parentheses (or a call to ko.utils.unwrapObservable).

    In your 2nd example the binding value is: selectedMessage() && selectedMessage().message().subject. When this expression evaluates to the subject property, KO sees that the evaluated value is an observable, and so it unwraps it for you. (Internally this is probably just a call to ko.utils.unwrapObservable).

    But, since the expression doesn't evaluate to the message property, which I'm assuming is also an observable, the parentheses for accessing that property are necessary (e.g. selectedMessage().message.subject wouldn't work).

提交回复
热议问题