封装在 requireJs 中的代码的调试接口

Debug interface for code wrapped in requireJs

我有一个 javascript 程序 apps.js,它像这样加载到我的 index.html 文件中:

<script data-main='apps.js' src='js/require.js'></script> 

其中 apps.js 是自动生成的代码,但包含一个 requirejs 模块。我想配置一个 test.html,它在加载 apps.js 之后加载一个 test.js 文件。目的是 test.js 让我可以快速测试来自 apps.js 的特定组件,所以我想将 apps.js 视为一个独立的库。

我有哪些选择?

我发现的一个选项是 test.html 仅在 data-main 行中加载 test.js,然后 test.js 在其顶部使用类似的内容:

$.ajaxSetup({async:false});
$.getScript("apps.js");
$.ajaxSetup({async:true});

但我不确定这是最好的方法。还有其他想法吗?谢谢!

你可以只需要你的 app.js 文件,假设你的 return 定义了一些东西。您的 test.js 会有这样的代码:

require(['app'], function(app) {

// You can now test stuff on app here.
assert(app.myFunction()).equals(1); // Will be true.

});

您的 app.js 文件可能包含如下内容:

require(function() {

 var myApp = {
    myFunction: function() {
     return 1;
    }
 };

return myApp;

});