我正在尝试创建一个绑定处理程序,它允许我跟踪绑定中使用的任何值是否已更改:
*
到目前为止我尝试了以下内容:
ko.bindingHandlers.dirty = { init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { var counter = 0; var dirtyObservable = valueAccessor(); var appliedBindings = false; var computed = ko.computed(function() { if (!appliedBindings) { // I was hoping this would subscribe all the used observables ko.applyBindingsToDescendants(bindingContext, element); appliedBindings = true; } // make sure subscribe is triggered by returning a new value return counter++; }); computed.subscribe(function() { dirtyObservable(true); }); return { controlsDescendantBindings: true }; } };
我希望ko.applyBindingsToDescendants
能订阅该容器内所有绑定中使用的observable.然而,情况似乎并非如此; 见http://jsfiddle.net/F3uMr/1/.
还有另一种方法来实现这一目标吗?或者为每个容器创建一个ViewModel然后使用脏标志会更好吗?