Knockout Subscribe to any change in observable complex object

后端 未结 1 591
無奈伤痛
無奈伤痛 2021-01-31 09:31

I have a viewmodel which contains an observable, which is initialized with an object. this object itself contains observables.

my goal is to be notified whenever that o

1条回答
  •  情歌与酒
    2021-01-31 09:59

    You can use the following trick:

    ko.computed(function() {
        return ko.toJSON(complexObject);
    }).subscribe(function() {
        // called whenever any of the properties of complexObject changes
    });
    

    See http://jsfiddle.net/xcajt4qn/3/

    The reason why this works is ko.toJSON will recursively read all the properties in the object, therefore making the computed depend on all the properties.

    0 讨论(0)
提交回复
热议问题