How to wait until it ends in dojo

I provide the infrastructure for other developers, and I use Dojo for this.

In my Init function, I use the "require" method (of course), which one of the parameters is the callback function when all modules are loaded.

The problem is that the client does not want to use callbacks. He wants to call me and call to use me (to synchronize the Init method) - and return the code to him after we have surely finished loading our modules.

My code

<script src=..../dojo.js></script> function Init() { var loaded = false; require(["...myFiles..."], function() { loaded = true; }); // Whatever to do here or some other way to hold till the require operation finished while (!loaded) // :) } 

My client side

  Init(); myFiles.DoSomething(); 

Can this be done? To require synchronization or do something else that waits for it to end? To return from the init method only after the require? Method completes

+1
source share
2 answers

You can either call your next function from the required response, or use a deferred object.

  function Init() { var d = new dojo.Deferred(); require(["...myFiles..."], function() { d.resolve(true); }); return d; } 

Another file

  Init().then(myFiles.DoSomething); 
+1
source

Make your initializer a module, say "my / Files", and make your init and doSomething functions static methods of this module:

in "my / Files.js"

 define([ "dojo/_base/declare", ...<your other required dependencies> ], function(declare){ var myFiles = declare(null, {}); myFiles.init = function(){ // Your initialization code } myFiles.doSomething = function(){ ... } return myFiles; }); 

In your html:

 <script type="dojo/require"> myFiles : "my/Files" </script> 

Then you can use it in your javascript, for example:

 myFiles.init(); myFiles.doSomething(); 
0
source

Source: https://habr.com/ru/post/1498599/


All Articles