我在使用 Durandal/Knockout 时遇到了一个奇怪的问题。在某些情况下,绑定无法正常工作。我已经简化了这个问题中出现的情况。
我在代码中的某处设置组合信息,例如:
compositionInfo({
model: viewModelInstance,
view: viewName,
activate: viewModelInstance.activate
});
这是我的观点:
<div id="service-container"
data-bind="compose: { model: compositionInfo().model,
view: compositionInfo().view,
activate: compositionInfo().activate}">
在第一次,组合工作正常。但是在下一次compositionInfo
更改时(使用相同的行和代码的相同位置),什么也没有发生。
第一次["Activating",...]
在日志窗口中有一个。但是在第二次没有这样的日志或["Binding"]
日志。
我跟踪了 Durandal 和 Knockout 代码,发现knockout-2.3.0.debug
文件中有一个evaluateImmediate()
函数在第一次运行此行(正确的):
var newValue = readFunction.call(evaluatorFunctionTarget);
并导致合成开始激活模型。
但是当它不起作用时,evaulateImmediate()
它会通过以下代码返回上面的一些行:
// Don't dispose on first evaluation, because the "disposeWhen" callback might
// e.g., dispose when the associated DOM element isn't in the doc, and it's not
// going to be in the doc until *after* the first evaluation
if (_hasBeenEvaluated && disposeWhen()) {
dispose();
return;
}
这段代码是干什么用的?如果我评论这些行,一切正常。
此问题因计算机而异。在大多数情况下,在我的计算机上它只是第一次工作。但在其他计算机上,它大部分时间都可以工作,大约 3/10 情况下会失败。
仅供参考,我正在使用 Durandal 1.1.1 和 Knockout 2.3.0