简体   繁体   English

Dojo需要,在模块加载失败时连接到错误

[英]Dojo require, connect to error when module failed loading

When I try to load nonexistent module, it fail with 404 error (of course). 当我尝试加载不存在的模块时,它失败并出现404错误(当然)。 I want to handle this error but don't know how to connect to "error" event. 我想处理此错误但不知道如何连接到“错误”事件。

According to Dojo doc, I should be able to do that using its micro event api . 根据Dojo doc,我应该可以使用它的微事件api来做到这一点。

This code does not work. 此代码不起作用。

var handle = require.on('error', function( error )
                 {
                     alert('Finally error')
                 });

require(['nonexistent/module'], function ( m )
{
     alert('Module was loaded correctly')           
});

Dojo version is 1.7.1, browser latest Chrome. Dojo版本是1.7.1,浏览器最新的Chrome。

The documentation on this one is a bit curious. 在这一个文档有点好奇。 I can get the 'error' event to fire after a short timeout (dojoConfig.waitSeconds) after a failed xhr require, but only in async mode, and only with a dev build of Dojo. 在xhr需要失败后,我可以在短暂超时(dojoConfig.waitSeconds)之后触发'error'事件,但只能在异步模式下,并且只能使用开发的Dojo构建。

Why? 为什么? Well, only async mode raises the event, and it looks like the CDN build was built with dojo-timeout-api == 0. 好吧,只有异步模式才会引发事件,看起来CDN构建是用dojo-timeout-api == 0构建的。

Can you use your own custom (SDK) version of Dojo? 你可以使用自己的自定义(SDK)版本的Dojo吗?

@mschr - your jsfiddle sample works if you use Dojo (nightly) instead of Dojo 1.7.2 - same reason as above. @mschr - 如果你使用Dojo(每晚)而不是Dojo 1.7.2,你的jsfiddle样本也可以工作 - 与上面相同的原因。

try it with debugging set 尝试使用调试集

<script src="{{baseUrl}}/dojo/dojo.js" data-dojo-config="async:true, debug: true, debugAtAllCost:true"></script>

Edit; 编辑; no such luck - it seems that docs are ahead of implementation here.. 没有这样的运气 - 似乎文档在这里实现了...

created a fiddle for the purpose of maybe finding a solution, concept is nice, would enjoy to use this myself 为了找到解决方案而创造了一个小提琴,概念很好,我很乐意自己使用它

http://jsfiddle.net/seeds/h6bXQ/ http://jsfiddle.net/seeds/h6bXQ/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM