View comments | RSS feed

Using the doLater() method

The doLater() method queues a function to be called when the selected operation finishes. The doLater() method is useful if you have functions or dynamically created controls that rely on nonsequential operations such as web services. Rather than try to time the return of data from a web service call, you can use the doLater() method to ensure that the necessary data is available before continuing.

The doLater() method is from the mx.core.UIObject class. The doLater() method has the following signature:

doLater(obj:Object, func: String, args: Array):Void

The obj argument is the object that contains the function. The func argument is the function to call on the object. The args argument is an optional array of arguments you can pass to the function.

The following example uses a call to the doLater() method to ensure that the current operation is completed before the createNext() custom method is called:

<?xml version="1.0">
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns:local="*" creationComplete="doLater(this,'createNext')">
   <mx:Script><![CDATA[
      var creationOrder = ["box1","box2","box3"];
      var creationIndex = 0;
      function createNext() {
         var nextObj = this[creationOrder[creationIndex++]];
         nextObj.addEventListener("childrenCreated", this);
         nextObj.createComponents();
         if (creationIndex < creationOrder.length) {
            nextObj.getChildAt(0).addEventListener("creationComplete", this);
         }
      }
      function handleEvent(e:Object):Void {
         if (e.type == "creationComplete") { 
            doLater(this, "createNext");
         } else {
            super.handleEvent(e);
         }
      }
   ]]></mx:Script>
   <mx:VBox id="box1" width="200" height="200" creationPolicy="none">
      <mx:DataGrid width="190" height="95">
         ... // Add data provider here.
      </mx:DataGrid>
   </mx:VBox>
   <mx:VBox id="box2" width="200" height="200" creationPolicy="none">
      <mx:DataGrid width="190" height="95">
         ... // Add data provider here.
      </mx:DataGrid>
   </mx:VBox>
   <mx:VBox id="box3" width="200" height="200" creationPolicy="none">
      <mx:DataGrid width="190" height="95">
         ... // Add data provider here.
      </mx:DataGrid>
   </mx:VBox>
</mx:Application>

Version 1.5

Comments


ntsiii said on Feb 3, 2005 at 8:46 AM :
Everything I understand tells me that the example in the first paragraph is wrong. WebService calls are asynchronous. doLater moves the function execution into the next "frame", correct? There is no way to know that an asynchronous data call will return within a single frame. Data service results must be handled in using the result event.
danger42 said on Feb 3, 2005 at 8:51 AM :
You are correct. I have already rewritten this section to be more accurate and better explain the doLater() method. This new doc will appear in the next edition of Flex.

Matthew Horn
Flex Documentation

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/15/flex_docs_en/00000487.htm