Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requirejs - construct a module when it is defined, not when it is required

I have a set of named requirejs modules with constructor-as-a-function.

define('myModule', [ 'import1', 'import2' ], function(i1, i2) {
    ...
});

I want to be able to

  • attach modules to HTML in corect order NOT USING requirejs API via script tags
  • attach and use modules USING requirejs API

Now, then I attaching a script with define call to a document, script is successfully loaded, but module is not getting to be defined, constructor is not called.

Is it a normal behaviour? Is there some workaround to fix this?

like image 816
Olegas Avatar asked Jul 05 '13 13:07

Olegas


1 Answers

script is successfully loaded, but constructor is not called. Is it a normal behaviour?

Yes. They are only executed when needed, the define might be renamed as register. Also it might need to wait for its dependencies anyway.

Is there some workaround to fix this?

If you want to execute it, just place a require() call for it (not in the same file though).

I have some legacy code which knows nothing about modules and depends on file attachment instead.

While the script attachment does successfully load them (synchronously!), they will be executed asynchronously. You better would wrap the legacy code in requires as well (which shouldn't break anything).

like image 74
2 revs Avatar answered Oct 26 '22 15:10

2 revs