【问题标题】:Dynamic composition using knockout and durandal doesn't work使用淘汰赛和杜兰达尔的动态构图不起作用
【发布时间】: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


【解决方案1】:

我在您的 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
});

【讨论】:

  • 事实上,代码和你之前的一样,也没有工作。顺便说一句,您有 3 个选项来设置activatetrue 用于默认绑定到模型的activatefalse 以避免激活,以及运行function 以激活运行而不是默认的activate 方法。
【解决方案2】:

正如我在问题中提到的,使用 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-16
    • 2013-07-20
    • 2013-03-16
    • 2015-05-25
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2014-05-10
    • 2014-06-19
    相关资源
    最近更新 更多