捕获模块加载错误并处理它们

编程入门 行业动态 更新时间:2024-10-24 09:30:59
本文介绍了捕获模块加载错误并处理它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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 + '\nrequirejs/docs/errors.html# Load timeout for modules: modules/messages/messages requirejs/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 的特定用途的自定义错误处理。这里记录了错误 requirejs/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 requirejs/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上的示例显示了人们如何处理他们想要为他们想要使用众所周知的标识符提供的资源尝试多个位置的情况。据推测,该示例中的整个代码库需要jQuery,需要jquery。无论jQuery恰好位于什么位置,它都可以作为jquery用于整个代码库。

The example at requirejs 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()方法。

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.

更多推荐

捕获模块加载错误并处理它们

本文发布于:2023-11-15 04:05:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   加载   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!