javascript require.js:访问所有加载的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11756483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
require.js: Access all loaded modules
提问by Michael
Is there any way to access all loaded modules of require.js?
有没有办法访问require.js的所有加载模块?
Background:
I want to automatically call an init()
function of my javascript-modules after all of them are loaded, see require.js + backbone.js: How to structure modules that have an initialize function?Without require.js I looped over my own module-storage and called each init()
function.
I now want to do this with require.js. I'm aware that calling a my_custom_init_function_favoritecolor_petname_love123
of every loaded module (including external libraries) is dangerous. I hope that this will cause less trouble than manually keeping a list of all modules (and the requirements of these modules) up-to-date. Forgetting one module init() is much more likely than a third-party library having my custom function name (though the latter is probably harder to debug).
背景:
我想init()
在加载所有 javascript 模块后自动调用它们的函数,请参阅require.js +backbone.js:如何构建具有初始化功能的模块?在没有 require.js 的情况下,我遍历了自己的模块存储并调用了每个init()
函数。
我现在想用 require.js 做到这一点。我知道调用my_custom_init_function_favoritecolor_petname_love123
每个加载的模块(包括外部库)是危险的。我希望这比手动保持所有模块的列表(以及这些模块的要求)最新会带来更少的麻烦。忘记一个模块 init() 比使用我的自定义函数名称的第三方库更有可能(尽管后者可能更难调试)。
Or does anyone have a better idea of how to accomplish that?
或者有没有人对如何做到这一点有更好的想法?
回答by AlexStack
Yes, require.s.contexts._.defined
is an object which its keys are the module names and the values include the value returned from that module.
是的,require.s.contexts._.defined
是一个对象,其键是模块名称,值包括从该模块返回的值。
require.s.contexts._.defined
includes all the modules (either define()
or require()
such as the Javascript file that is the starting point of the program and is indicated using data-main
for RequireJS).
require.s.contexts._.defined
包括所有的模块(或者define()
或require()
诸如JavaScript文件是程序的开始点,并使用被指示data-main
为RequireJS)。
回答by Volker Rose
Just finished a similar behavior within my RequireJS project. require.s.contexts['_'].registry
holds the list of the registered modules.
刚刚在我的 RequireJS 项目中完成了一个类似的行为。require.s.contexts['_'].registry
保存已注册模块的列表。
I am using Underscore.js
for getting, filtering and iterating the list of modules. Maybe the following code fragment helps you:
我Underscore.js
用于获取、过滤和迭代模块列表。也许以下代码片段可以帮助您:
var modules_registered = _.keys(require.s.contexts['_'].registry);
var modules_to_be_initialized = _.filter(modules_registered, function(module_title) {
return module_title.indexOf('modules') > -1;
});
_.each(modules_to_be_initialized, function(module_name) {
require([module_name], function(current_module) {
current_module.init();
});
});
回答by Zar Shardan
I'm lazy, so I just did this:
我很懒,所以我只是这样做:
var modulesToLoad = Object.keys(require.s.contexts['_'].registry);
require(modulesToLoad);
Based on other answers here.
基于此处的其他答案。