require.js:访问所有已加载的模块 [英] require.js: Access all loaded modules

查看:505
本文介绍了require.js:访问所有已加载的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法访问require.js的所有已加载模块?

Is there any way to access all loaded modules of require.js?

背景:

我想自动调用 init()加载完所有javascript-modules后我的javascript-modules的功能,请参阅 require.js + backbone.js:如何构造具有初始化函数的模块?
没有require.js我遍历我自己的模块存储并调用每个 init() function。

我现在想用require.js做这个。我知道调用每个加载模块(包括外部库)的 my_custom_init_function_favoritecolor_petname_love123 是危险的。我希望这比手动保持所有模块的列表(以及这些模块的要求)是最新的麻烦少。忘记一个模块init()比具有我的自定义函数名的第三方库更有可能(虽然后者可能更难调试)。

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).

或者是否有人更好地了解如何实现这一目标?

Or does anyone have a better idea of how to accomplish that?

推荐答案

是, require.s.contexts._ .defined 是一个对象,它的键是模块名称,值包括从该模块返回的值。

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 包括所有模块( define() require(),例如作为程序起点的Javascript文件,并使用 data-main 表示RequireJS。

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.js:访问所有已加载的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆