【问题标题】:Why isn't my Dojo ContentPane resolving its promise after loading content?为什么我的 Dojo ContentPane 在加载内容后没有解决它的承诺?
【发布时间】:2016-08-25 08:24:23
【问题描述】:

我正在努力修复一个相对较旧的应用程序中的错误,该应用程序是为旧版本的 Dojo 编写的,即使在那时也无法正常工作,现在正在 Dojo 1.10 上运行。一些应用程序逻辑对于当前的 Dojo 范例来说并不完全正确,但其中一些对我来说似乎是正确的,我无法弄清楚为什么它没有按预期工作。

有一个基于dijit.layout.ContentPane 的自定义小部件。添加的方法似乎都没有破坏原始方法,仅用于通过主题将一些滚动事件连接到其他小部件。还有一个topic可以触发ContentPane中的地址变化。

相关位看起来像这样:

// The widget is initially setup declarative markup
var contentPaneWidget = registry.byId("<content_pane_id>");

// This subscribe is actually part of the custom widget code, but
// the topic subscription and resulting set() work, so I don't think
// where this is run is relevant to the question
topic.subscribe("navEvent", function(url) {
    contentPaneWidget.set("href", url).then(function() {
        topic.publish("navComplete", url);
    }, function(err) {
        console.log("Navigation error:", err);
    });
});

// This topic subscription in another widget should get fired after
// content loads in the ContentPane based widget 
topic.subscribe("navComplete", function(url) {
    console.log("Navigation finished:",  url);
});

// Test the chain ov events by firing off a new URL to the nav topic
topic.publish("navEvent", "<new_content_url>");

内容正确加载。 URL 会被解析和加载,并且内容窗格会使用下载的内容进行更新。 问题是.then() 函数永远不会触发,因此 navComplete 主题永远不会被激活。有趣的是,不仅succeed 函数没有触发,error 函数也没有触发。承诺刚刚开放。

如果我手动解决承诺(通过将.resolve() 添加到链的末尾或稍后在代码中或从控制台手动调用contentPaneWidget.onLoadDeferred.resolve()),那么一切都会按预期启动。 As documented set("href", ...) 正在返回一个promise,在网络操作和内容渲染完成后,promise 永远不会被解析。

这是不正确的用法吗? Dojo 中是否存在错误?还是我在此应用程序代码中寻找其他一些 gremlin?

【问题讨论】:

  • 在 try/catch 中的延迟解析。我猜您正在加载的内容会引发 JS 错误,并且该错误已被 try catch 吞噬,因此您什么也看不到
  • @ben Cha-ching 我们赢了!应用程序中围绕set('href', ...) 的整个代码块都包含在一个try 语句中,该语句干扰了Dojo 正在执行的任何操作。我删除了 try 大量的 try 块,并将其替换为仅包装已知会引发异常的两行,并且该应用程序突然让其所有主题频道再次运行!您能否将可能的尝试/接受交互问题记入答案中以便我接受?
  • 太棒了!在大多数情况下,try/catch 与其说是解决方案,不如说是一个问题;)

标签: javascript dojo promise dijit.layout


【解决方案1】:

try/catch 中的延迟解析。
https://github.com/dojo/dijit/blob/master/layout/ContentPane.js#L440

我猜你正在加载的内容会引发一个 JS 错误,并且这个错误被 try/catch 吞噬,因此你看不到任何事情发生......

【讨论】:

    猜你喜欢
    • 2018-07-09
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多