【发布时间】:2013-11-14 02:10:38
【问题描述】:
我在使用 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
【问题讨论】:
-
我建议升级到 Durandal 2.0.1,因为对 compose 绑定进行了许多增强。 2.0 有一些重大变化,因此需要一些额外的工作durandaljs.com/documentation/Conversion-Guide
-
@RainerAtSpirit 目前由于我的项目时间限制。不可能同时升级。顺便说一句,似乎问题不在于杜兰达尔。代码在淘汰赛中。
-
听起来你的绑定在其他地方坏了。我一直使用相同的构图方法
-
@PWKad 在简单的情况下它对我来说工作正常。但是我有很多复杂的嵌套绑定,这会导致在某些情况下随机出现这个错误。但评论该代码。一切正常。
标签: knockout.js durandal knockout-2.0 durandal-navigation