【发布时间】:2017-05-13 12:51:11
【问题描述】:
我知道get the new value of an observable in a subscribe event 有解决方案(在实际更改之后),但我想知道是否可以在“beforeChange”订阅中访问 observable 的新值。
下面是current version of Knockout (3.4.1) 的 sn-p,分别使用旧值和新值引发 beforeChange 和 afterChange 订阅处理程序:
if (computedObservable.isDifferent(state.latestValue, newValue)) {
if (!state.isSleeping) {
computedObservable["notifySubscribers"](state.latestValue, "beforeChange");
}
state.latestValue = newValue;
if (DEBUG) computedObservable._latestValue = newValue;
if (state.isSleeping) {
computedObservable.updateVersion();
} else if (notifyChange) {
computedObservable["notifySubscribers"](state.latestValue);
}
changed = true;
}
显然 newValue 在“beforeChange”中可用,所以我想分叉是一种选择,但我不想这样做。
有没有其他解决方案来获得这个新值“beforeChange”?
【问题讨论】:
-
您能否详细说明为什么要在设置新值之前获取它?
-
@MichaelBest 我试图阻止 FOUC。当我能够在实际设置之前读取新值时,我可以更新一些其他状态以防止不需要的视图中间渲染
-
您应该能够使用计算或订阅来更新可用于隐藏/显示/样式的第二个可观察对象。例如:jsfiddle.net/dmnqoc53