Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
53
rated 0 times [  60] [ 7]  / answers: 1 / hits: 20926  / 12 Years ago, wed, august 1, 2012, 12:00:00

Is there any way to access all loaded modules of 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).



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


More From » requirejs

 Answers
82

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


[#83928] Tuesday, July 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
agustindejonm

Total Points: 738
Total Questions: 84
Total Answers: 84

Location: Northern Ireland
Member since Mon, Nov 14, 2022
2 Years ago
agustindejonm questions
Fri, Jun 25, 21, 00:00, 3 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Sat, May 16, 20, 00:00, 4 Years ago
;