对 Flex/Actionscript 方法/函数方法调用进行排序的最佳方式

发布于 2024-11-28 22:44:11 字数 1161 浏览 2 评论 0原文

好的,据我所知,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你再特殊 2024-12-05 22:44:11

查看来自 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.

原来分手还会想你 2024-12-05 22:44:11

我喜欢用 Lisp-y 的方式解决这个问题:

// tell this function to start creating. 
private function initContainers( count:int ):void
{
     if( count <= 0 ) return;
     var newContainer:CustomerContainer = new CustomerContainer();
     newContainer.build();
     /* ... stuff happens ... */ 
     newContainer.addEventListener( FlexEvent.CREATION_COMPLETE,
                                    function( fe:FlexEvent ):void
                                    {
                                        // tell the function to create one fewer
                                        initContainers( count - 1 );
                                    } );
}

你的序列:

  1. initContainers( x ) ->
    1. 创建容器。
    2. 等待事件 ->
  2. initContainers( x - 1 );
    1. 创建容器。
    2. 等待事件 ->

I like solving this the Lisp-y way:

// tell this function to start creating. 
private function initContainers( count:int ):void
{
     if( count <= 0 ) return;
     var newContainer:CustomerContainer = new CustomerContainer();
     newContainer.build();
     /* ... stuff happens ... */ 
     newContainer.addEventListener( FlexEvent.CREATION_COMPLETE,
                                    function( fe:FlexEvent ):void
                                    {
                                        // tell the function to create one fewer
                                        initContainers( count - 1 );
                                    } );
}

Your sequence:

  1. initContainers( x ) ->
    1. create container.
    2. wait for event ->
  2. initContainers( x - 1 );
    1. create container.
    2. wait for event -> <!-- lots o' more -->
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文