9 Replies Latest reply on Aug 7, 2009 4:11 PM by Gregory Lafrance

    SEQUENCE OF EXECUTION IN FLEX 3

    dshterio

      Hi everybody, I am working with flex on two projects up for now. I have noticed the unsequentional execution (call) of different procedures in action script 3, but just now this is making a big problem in my application.

      In details:

      I have a google earth map, and I need to center it on a specific point.

      So I have created a class that calls a http service to get the center from the data base.

      What generally I have done is the following:

       

      package scripts
      {  

       

           impots....
         
          public class MapCenter
          {
              private var http1:HTTPService = new HTTPService();
              public var center:LatLng;
              private var RouteID:Number = new Number();
            
             
              public function MapCenter(ID:Number)
              {
                  RouteID = ID;
                  http1.addEventListener(ResultEvent.RESULT, fnResultHandler);
                   http1.addEventListener(FaultEvent.FAULT, fnFaultHandler);
                    http1.url = "php/getMapCenter.php";
                  var obj:Object = new Object();
                  obj.RouteID = ID;
                  http1.send(obj);
              }

        • 1. Re: SEQUENCE OF EXECUTION IN FLEX 3
          Gregory Lafrance Level 6

          From the code you posted and your description, perhaps I do not fully understand your problem, but events in Flex are asynchronous, so you can't assume that data is back from the database and do something else with it, you often need to do something or call a method inside the result handler because then you know the data is available.

           

          If this post answered your question or helped, please mark it as such.

          • 2. Re: SEQUENCE OF EXECUTION IN FLEX 3
            Flex harUI Adobe Employee

            To be picky, events are not asynchronous, server requests are.  You call send() and sometime much later, you'll get an event from the server.  Once that event is dispatched, all handlers for the event are called synchronously

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            • 3. Re: SEQUENCE OF EXECUTION IN FLEX 3
              dshterio Level 1

              Because I had problem with the internet connection my problem wasn't posted fully. So, as addition to the earlier post I want to add that in the function dispatched by the result listener I have a variable set. It is the variable center which was declared earlier. When in my main application (because all this code is in AS class) I call a method that returns the data in the "center" variable I get nothing. Although sometimes if there is a delay due to some reasons the center is set correctly. So actually ,  my question is: May I do something to place somekind of listener for this kind of variables that are related to httpservices and other similar things?

              Thank you all once again.

              Have a nice evening.

              • 4. Re: SEQUENCE OF EXECUTION IN FLEX 3
                Gregory Lafrance Level 6

                Not exactly clear on the sequence of your function calls, event listeners, and their relationships.

                 

                Perhaps you can clarify this.

                • 5. Re: SEQUENCE OF EXECUTION IN FLEX 3
                  dshterio Level 1

                  I am sorry,

                  I will try to post my problem as clear as I could.

                  Basically there is an Action Script class that the main application calls, like this: var p1:Path = new Path(); in this class different http services are called so that the final result should be produced. Meanwhile (something that shouldn't be happening until the p1 object hasn't retrieved all data) the other parts of the main application are executing - the map is being created, other http services are being called etc. When the time comes to place the result from p1 on the map it appears that it hasn't been created still.

                  So my question is if there is some way like the event listeners to know when the data retrieving from p1 is finished.

                  And then I would be able to place correctly the data on the map.

                  Thanks once again.

                  • 6. Re: SEQUENCE OF EXECUTION IN FLEX 3
                    Gregory Lafrance Level 6

                    Create a custom event, and when p1 has its data, dispatch the event.

                     

                    The main app or some component can listen for that event, and when the listener is called p1 is placed on the map.

                     

                    If for some reason the listener is not being called correctly, it could be that the component listening for the event is not in the same exact display list hierarchy as the class for p1, and maybe p1 class it not even a visual class, so when in doubt, add the event listener to the systemManager.

                     

                    Here is my Flex 3 Cookbook post on custom events, and I show adding event listener to systemManager:

                     

                    http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=2&postI d=11246

                     

                    If this post answered your question or helped, please mark it as such.

                    1 person found this helpful
                    • 7. Re: SEQUENCE OF EXECUTION IN FLEX 3
                      dshterio Level 1

                      Thank you very much , I will read the cookbook and then hopefully will manage my problem Thanks once again.

                      • 8. Re: SEQUENCE OF EXECUTION IN FLEX 3
                        dshterio Level 1

                        OK, so thanks to the replays to my question in this discussion I have managed to create what I was trying to do, in exact - to synchronize the result of the httpservices with the main application and a map from google maps api. And for other people who may need this kind of help I will post my solution.

                         

                        1. Create a custon event class:

                         

                        package scripts.CustomEvents
                        {
                            import flash.events.Event;
                           
                            import mx.controls.Alert;

                         

                            public class PathEvent extends Event
                            {
                                public static const COMPLETE:String = "complete";
                                public function PathEvent(type:String)
                                {
                                    super(type);
                                } 
                            }
                        }

                         

                        2. In a class in which I gather the data from the httpservice I have a variable of type EventDispathcer

                         

                        var dE:EventDispatcher = new EventDispatcher();

                         

                        3. I have a httpService which has the following:

                         

                        var h1:HTTPService = new HTTPService();

                        h1.url = "myDataRetrieval.php";

                        h1.addEventListener(ResultEvent.RESULT, fnGetResult);

                        h1.send();

                         

                        4. The fnGetResult function:

                         

                        private function fnGetResult(e:ResultEvent):void

                        {

                             ... do something with the result...

                             dE.dispatchEvent(new PathEvent(PathEvent.COMPLETE));

                        }

                         

                        in this way when the result is processed then the event will be triggered. So when it is triggared every listener that is listening for it will execute its function

                         

                        5. Add in the application or in other component where is needed an event listener:

                         

                        eg. in the main application:

                         

                        var p1:Path = new Path();

                         

                        (in the Path class we have tha code written in 3 and 4.)

                         

                        p1.dE.addEventListener(PathEvent.COMPLETE, fnListenPath);

                        in the fnListenPath we put whatever we want to be done when the result from the httpServices is ready.

                         

                        6. The fnListenPath function:

                         

                        private function fnListenPath(e:PathEvent):void

                        {

                             Alert.show("Result is ready to be processed");

                        }

                         

                        So I hope that I hava helped in some way to somebody.

                        If needed other help with this (or anything else) send me an e-mail to my address.

                        Have a nice coding.

                        • 9. Re: SEQUENCE OF EXECUTION IN FLEX 3
                          Gregory Lafrance Level 6

                          Looks like my post and link to the cookbook entry on custom events may have helped. Perhaps you can attribute that post of mine as helping to answer your question.