Detecting change to Knockout view model

后端 未结 9 2404
情歌与酒
情歌与酒 2020-12-04 07:38

Sure this is a very easy question to answer but is there an easy way to determine if any property of a knockout view model has changed?

9条回答
  •  盖世英雄少女心
    2020-12-04 07:54

    You might use the plugin below for this:

    https://github.com/ZiadJ/knockoutjs-reactor

    The code for example will allow you to keep track of all changes within any viewModel:

    ko.watch(someViewModel, { depth: -1 }, function(parents, child) { 
        alert('New value is: ' + child());
    });
    

    PS: As of now this will not work with subscribables nested within an array but a new version that supports it is on the way.

    Update: The sample code was upgraded to work with v1.2b which adds support for array items and subscribable-in-subscribable properties.

提交回复
热议问题