How to wait till require finished in dojo

試著忘記壹切 提交于 2019-12-04 20:16:48

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

 function Init()
 {
    var d =  new dojo.Deferred();

    require(["...myFiles..."], 
       function()
       {
           d.resolve(true);
       });
   return d;
 }

Other file

 Init().then(myFiles.DoSomething);

Make your initializer be a module, say "my/Files", and make your init and doSomething functions be static methods of that 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 like :

myFiles.init();
myFiles.doSomething();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!