please help me in understanding why this computed observable is not able to access observables.

前端 未结 3 1939

this is the fiddle - http://jsfiddle.net/iRamesh/36N4m/

Not sure why computed observable is not returning any value. I know how to make it working but not sure why t

3条回答
  •  青春惊慌失措
    2021-01-01 06:44

    In addition to the solution from @RPNiemeyer, there is yet another alternative:

    var viewModel = {
        firstName: ko.observable("r"),
        lastName: ko.observable("j"),
        fullName: ko.computed({
            read: function() { return viewModel.firstName(); },
            deferEvaluation: true
        })
     };
    

    The computed callback refers to the viewModel variable from the outer scope, and deferEvaluation makes sure the computed will only be called when needed, because at creation time the viewModel variable won't be ready.

提交回复
热议问题