捕获模块加载错误并处理它们 [英] Catching module loading errors and processing them
问题描述
我正在尝试使用require.js加载一些内容。如果内容不存在,我想抓住错误并通知用户。
I am trying to load some content using require.js. If the content doesn't exist I'd like to catch the error and notify the user.
在firebug中我可以看到两个错误:
In firebug I can see two errors:
NetworkError:404 Not Found
"NetworkError: 404 Not Found
...然后a几秒钟后:
...and then a few seconds later:
var e = new Error(msg + '\nhttp://requirejs.org/docs/errors.html#
Load timeout for modules: modules/messages/messages
http://requirejs.org/docs/errors.html#timeout
我的代码类似于:
require([path], function(content){
//need to catch errors as this will not be called;
});
如何绑定到requirejs事件?有什么想法吗?
How would one bind to requirejs events? Any idea?
推荐答案
也可以使用errbacks具有适合于 require
的特定用途的自定义错误处理。这里记录了错误 http://requirejs.org/docs/api.html#errbacks 。基本上,如果加载失败,您可以添加 require
要调用的函数。如果加载成功,它会在调用函数之后。
It is also possible to use errbacks to have customized error handling appropriate to the specific use of require
. Errbacks are documented here http://requirejs.org/docs/api.html#errbacks. Basically, you can add to require
a function to be called if the load fails. It comes right after the function to be called if the load is successful.
Chin的案例可以处理为:
Chin's case could be handled as:
require([path], function(content){
//need to catch errors as this will not be called;
}, function (err) {
//display error to user
});
以下是尝试从多个地方加载的示例:
Here's an example that tries loading from multiple places:
require([mode_path], onload, function (err) {
if (mode_path.indexOf("/") !== -1)
// It is an actual path so don't try any further loading
throw new Error("can't load mode " + mode_path);
var path = "./modes/" + mode_path + "/" + mode_path;
require([path], onload,
function (err) {
require([path + "_mode"], onload);
});
});
在此示例中 onload
将是函数一旦所需的代码加载就调用, mode_path
是一个标识模式的字符串。你看到的是代码试图从3个不同的位置为编辑器加载模式模块。如果 mode_path
是 foo
,它将尝试加载 foo
,然后 ./ modes / foo / foo
然后 ./ modes / foo / foo_mode
。
In this example onload
would be the function called once the required code loads, and mode_path
is a string identifying the mode. What you see there is code attempting to load a mode module for an editor from 3 different locations. If mode_path
is foo
, it will try to load foo
, then ./modes/foo/foo
and then ./modes/foo/foo_mode
.
requirejs.org上的示例显示了人们如何处理他们想要为他们想要使用众所周知的标识符提供的资源尝试多个位置的情况。据推测,该示例中的整个代码库需要jQuery,需要jquery。无论jQuery恰好位于什么位置,它都可以作为jquery用于整个代码库。
The example at requirejs.org shows how one might handle a case where they want to try multiple locations for a resource they want to make available with a well-known identifier. Presumably the entire code-base in that example requires jQuery by requiring "jquery". Whatever location jQuery happens to be located at, it becomes available to the whole code-base as "jquery".
我的例子并不关心使模式知道整个代码库通过一个众所周知的标识符,因为在这种特定情况下,没有充分的理由这样做。 onload
函数将它获取的模块存储到一个变量中,其余的代码库通过调用 getMode()$ c $来获取它c>方法。
My example does not care about making the mode known to the entire code-base through a well-known identifier because in this specific case there's no good reason to do so. The onload
function stores the module it gets into a variable and the rest of the code base gets it by calling a getMode()
method.
这篇关于捕获模块加载错误并处理它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!