在我的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完成
发布评论