如何等待,直到要求在dojo完成 [英] How to wait till require finished in dojo

查看:322
本文介绍了如何等待,直到要求在dojo完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为其他开发人员提供基础架构,而我正在使用Dojo。



在我的Init函数中,我使用'require'方法(当然)其中一个参数是所有模块加载后的回调函数。



问题是客户端不想使用回调。他想要打电话给我,并使用我(使我的Init方法同步) - 并且在我们确定完成加载我们的模块后,给他代码。



我的代码

 < script src = .... / dojo.js>< / script> 

函数Init()
{
var loaded = false;

require([... myFiles ...],
function()
{
loaded = true;
});

//无论做什么或其他一些方法,直到require操作完成
while(!loaded)// :)
}

我的客户端

  Init (); 
myFiles.DoSomething();

有可能吗?
要使同步或做别的事情等待,直到结束?
只有在require方法完成后才能从init方法返回?

解决方案

您可以调用下一个函数在需求响应中,或使用延迟对象。

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

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

其他文件

  Init()。then(myFiles.DoSomething); 


I'm providing an infrastructure for other developers, and i'm using Dojo for it.

In my Init function i'm using the 'require' method (of course) which one of the parameters are the callback function when all the modules have been loaded.

The problem is that the client don't want to use callbacks. He wants to call me and line after to use me (to make my Init method synchronized) - and give him the code back after we for sure 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();

Is it possible to do it ? To make require synchronized or do something else that waits till it's over ? To return from the init method only when the require method finished ?

解决方案

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);

这篇关于如何等待,直到要求在dojo完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆