F
F
Finom2014-05-15 15:45:39
JavaScript
Finom, 2014-05-15 15:45:39

Requirejs: how to call a module immediately?

I am writing an embedded application, something like a huge jQuery plugin. The "master" module must request another module, which in turn loads other modules. It (the main module) should create a global function. The problem is that this function is created with a delay due to the fact that the modules are asynchronous.
main.js:

define( 'main', [ 'blah' ], function( Blah ) {
  window.Blah = Blah;
});
require( ['main'] );

Next, in another file, we call: The optimizer should produce one JS file, which should immediately create a variable.
new Blah(); // Blah is not defined
<script src="blah-built.js"></script>
<script>new Blah(); // Blah is not defined </script>

Now you have to pervert like this:
main.js:
( function() {
  window.Blah = function() {
    var _this = this;
    require([ 'blah' ], function( _Blah ) {
      _this.me = new _Blah();
    });
  };
})();

How to solve this problem? It would be desirable to have the normal designer right after loading.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Vasilchuk, 2014-05-15
@Anonym

window.Blah = Blah;
You just broke the whole concept of requirejs.
Gotta change something

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question