0

I am trying to map a list of data to my array as following:

this.data = [];
_.map(dataList, function(element){
    var logic = require('./'+element+'/'+element);
    var tempalte = require('text!./' + element + '/' + element + '.html');
    return { Logic : logic, Template : tempalte };
});

I keep getting the following error when trying it: "Module name "..." has not been loaded yet for context: _ http://requirejs.org/docs/errors.html#notloaded"

what am i doing wrong here? is there no option to use underscore map with require?

Thanks

3
  • Should you not assign return value to something?
    – Rajesh
    Commented Jun 5, 2016 at 7:40
  • If you have read the linked docs you would know what the problem is. Commented Jun 5, 2016 at 7:41
  • andlrc - thats not a very helpful comment, I have read the linked doc but still wasnt able to make it work, if you have any idea why and how can i get it to work please do tell. Commented Jun 5, 2016 at 7:49

1 Answer 1

1

require.js actually analyzes your code statically when loading it, and searches for modules to be loaded. Actually this happens because require.js implements the AMD (asynchrouns module definition) specification and not CommonJS. This syntax is possible just as a helper (or syntactic sugar) for us as developers. This way, when your parent module is loaded, actually all its dependencies are loaded too, and only then require.js "runs" it, which gives you the sense of synchronous loading (or at list the syntax of such).

TL;DR: You cannot load modules dynamically using this syntax.

In my opinion, loading modules dynamically like that is not a very good practice, as I'm not sure that you'll be able to optimize it using r.js.

Not the answer you're looking for? Browse other questions tagged or ask your own question.