1

我在使用 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

4

2 回答 2

0

我在您的 compositioninfo observable 中看到了一个问题。activate 的值应该是 true 或 false,并且 viewModelInstance.activate 函数本身将由组合绑定找到/调用。

这是相关文档的链接 - https://github.com/BlueSpire/Durandal/blob/master/docs/1.2/Composition.html.md#activate

这只是您尝试创建代码的简化版本的错字/问题吗?

compositionInfo({
    model: viewModelInstance,
    view: viewName,
    activate: true
});
于 2013-11-03T23:35:35.537 回答
0

正如我在问题中提到的,使用 durandal 1.2 进行正确绑定的唯一方法是推荐以下几行:

// 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;
}

但是在升级到 之后Durandal 2.0.1,这些注释行会导致一些激活不止一次发生。

所以请记住,如果您升级到 2.0.1,请取消注释这些行或只获取原始knockout代码。

于 2013-11-28T11:07:05.250 回答