为什么我的Dojo ContentPane在加载内容后没有解决它的承诺?

Why isn't my Dojo ContentPane resolving its promise after loading content?

本文关键字:解决 承诺 Dojo 我的 ContentPane 加载 为什么      更新时间:2023-09-26

我正在努力修复一个相对较旧的应用程序中的错误,该应用程序是为旧版本的Dojo编写的,即使在那时也从未工作过,现在运行在Dojo 1.10上。有些应用程序逻辑不太适合当前的Dojo范例,但有些对我来说似乎是正确的,我不明白为什么它没有像预期的那样工作。

有一个基于dijit.layout.ContentPane的自定义小部件。添加的方法似乎都没有破坏原来的方法,只是通过主题将一些滚动事件连接到其他小部件。还有一个主题可以触发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主题永远不会激活。有趣的是,不只是成功函数没有触发,错误函数也没有触发。

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

这是不正确的用法吗?Dojo中有bug吗?还是我在这个应用程序代码中寻找其他一些小妖精?

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

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