开发者

When does a dependentObservable's function get called?

开发者 https://www.devze.com 2023-03-31 07:59 出处:网络
I have following sample code: <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">

I have following sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
   <script type="text/javascript" src="../knockout-1.2.1.js"></script>
   <script type="text/javascript" src="../knockout.mapping.js"></script>
</head>
<body>
   <div>Nested Object Value1 <input data-bind="value: NestedObject.Value1" type="text" /></div>
   <div>Value2 <input data-bind="value: Value2" type="text" /></div>

<hr/>
<div d开发者_开发问答ata-bind="text: ko.toJSON(viewModel)"></div>

<script type="text/javascript">
   var initialData = {"NestedObject":{"Value1":"Dummy value"}, "Value2" : "Hello world"};

   var viewModel = ko.mapping.fromJS(initialData);

   viewModel.ParameterUpdatedDependentObservable = ko.dependentObservable(function() {
        alert("dependentObservable fired");
    }, viewModel);

   ko.applyBindings(viewModel); // Makes Knockout get to work
   </script>
</body>
</html>

What I expected is that to see a popup to alert value is changed by user. When I run it, the popup shows up only once when page is initially loaded. Afterwards I can make value update but alert is no long triggered.

Any clue why?


dependentObservables track the observables that have their values accessed during evaluation to create dependencies.

In your case, your dependentObsevable does not access any observables. If you access the value of one or more of your observables in your code, then you will see it trigger whenever any of its dependencies are updated.

0

精彩评论

暂无评论...
验证码 换一张
取 消