我正在努力修复一个相对较旧的应用程序中的错误,该应用程序是为旧版本的 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 被解析并加载,内容 Pane 会使用下载的内容进行更新。 问题是 .then()
函数永远不会触发,因此 navComplete 主题永远不会激活。有趣的是,不仅成功函数没有触发,错误函数也没有触发。这个 promise 只是悬而未决。
如果我手动解决 promise (通过将 .resolve()
添加到链的末尾或稍后在代码中调用 contentPaneWidget.onLoadDeferred.resolve()
或从控制台手动)然后一切都会按预期启动。 As documented set("href", ...)
返回一个 Promise,在网络操作和内容渲染完成后,该 Promise 永远不会被解析。
这是错误的用法吗?道场有bug吗?或者我是在这个应用程序代码中寻找其他一些小 Sprite 吗?
最佳答案
try/catch
中延迟的解析。
请参阅https://github.com/dojo/dijit/blob/master/layout/ContentPane.js#L440
我猜您正在加载的内容会引发 JS 错误,并且该错误被 try/catch
吞没,因此您看不到任何事情发生...
关于javascript - 为什么我的 Dojo ContentPane 在加载内容后没有兑现其 promise ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39140194/