对 Flex/Actionscript 方法/函数方法调用进行排序的最佳方式
好的,据我所知,Flex 中的动作脚本是异步运行的。我需要能够在循环中同步运行方法。考虑以下代码:
public class CustomerContainer extends VBox
{
public function CustomerContainer ()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE,
this_creationCompleteHandler);
}
public function build():void
{
//process something here
}
}
现在我们的 MXML 文件中有一个 for 循环
<mx:Script>
<![CDATA[
private var newContainer:CustomerContainer;
public function foo():void
{
for (var i:int = 0; i < EndingNumber; i++)
{
newContainer = new CustomerContainer();
newContainer.build();//I need this to finish before it goes on to the
// next one. here is where the problem is flex runs through this loop
// creating each new object but I do not know if the first 1 object is
// complete before it begin processing the second one in the For Loop.
}
}
]]>
</mx:Script>
这不是我的具体情况,因为这里解释起来有点复杂。有一个抽象类和几个从它派生的自定义视图对象。有些视图依赖于其他视图首先完成,但我似乎无法按正确的顺序排列它们。定时器不是一个选项。可能没有正确解释这一点。
OK, from what I know actionscript in Flex runs asynchronously. I need the ability to run a method in a loop synchronously. Consider this code:
public class CustomerContainer extends VBox
{
public function CustomerContainer ()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE,
this_creationCompleteHandler);
}
public function build():void
{
//process something here
}
}
Now we have a for loop in our MXML file
<mx:Script>
<![CDATA[
private var newContainer:CustomerContainer;
public function foo():void
{
for (var i:int = 0; i < EndingNumber; i++)
{
newContainer = new CustomerContainer();
newContainer.build();//I need this to finish before it goes on to the
// next one. here is where the problem is flex runs through this loop
// creating each new object but I do not know if the first 1 object is
// complete before it begin processing the second one in the For Loop.
}
}
]]>
</mx:Script>
This is not my exact situation as it is a bit more complicated to explain here. There is an abstract class and several custom view objects derived from it. Some of the views are dependent on others being completed first but I cannot seem to order them in the correct order. TIMERS are not an option. Probably not explaining this correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看来自 Cairngorm 的 Task 库。
它提供了一种对工作单元进行排序并让它们按顺序、并行或同时运行的方法。
否则,处理事件侦听器和错误情况很快就会失控。
Take a look at the Task library from Cairngorm.
It provides a way to order units of work, and have them run sequentially, in parallel, or both.
Otherwise, dealing with event listeners, and error conditions quickly gets out of hand.
我喜欢用 Lisp-y 的方式解决这个问题:
你的序列:
I like solving this the Lisp-y way:
Your sequence:
<!-- lots o' more -->