Is there a way to define a module that dynamically load other modules in RequireJS? If yes, how the optimizer (r.js) understands how/when a module has to be included?
For example, let dynModules
a module which defines name/path pairs:
define([], function () {
return ['moduleA', 'moduleB']; // Array of module names
});
Another module is going to load modules dynamically, based on the array. This will not work:
define(['dyn_modules'], function (dynModules) {
for(name in dynModules) {
var module = require(path); // Call RequireJS require
}
// ...
});
... gives me:
Uncaught Error: Module name moduleA has not been loaded yet for
context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
I can solve the error, but it's not dynamic anymore:
define(['dyn_modules', 'moduleA', 'moduleB'], function (dynModules) {
for(name in dynModules) {
var module = require(path); // Call RequireJS require
}
// ...
});