13 Replies Latest reply on Sep 14, 2010 12:58 PM by Flex4Education

    Problem with execution order

    Flex4Education Level 1

      Hi @ All

       

      I've a class with a constructor. This constructors calls some webservices etc to fill in his attribute data automatically.


      Now i need Flex to wait for finish all this things, because i read the attribute datas out of the class after some other operations.

      This works well, if there are not to much data to catch in the class. Else the Programm will read incomplete things out of my class attributes.

       

      Is there a way to say something like, wait for creationComplete ?

       

      thx F4E

        • 1. Re: Problem with execution order
          rootsounds Level 4

          What you're describing is a need for an event to signal when the attributes of some class are ready to be read. Go for it and create the event. Dispatch your event when appropriate and of course have something listening for it.

          • 2. Re: Problem with execution order
            Flex4Education Level 1

            Hi

             

            why does flex not automatically wait for finish the execution ?

            Is there any usefull idea behind it?

             

            And how could i add a event to a AS3 Class (from the parent class, in flex 3.5)? (i've never worked with custom events)

             

            I have a solution with a bool attribute in the class, which chance to true if everything is finish.

            But unfortunaly it needs a while(true) to handle this, and i don0t like while(true)'s in a webapplication...

             

            F4E

             

            Message was edited by: Flex4Education

            • 3. Re: Problem with execution order
              MikisMM

              And how could i add a event to a AS3 Class (from the parent class, in flex 3.5)? (i've never worked with custom events)

              Could you explain a bit? I don't quite catch you here.

              why does flex not automatically wait for finish the execution ? Is there any usefull idea behind it?

              Network communication is of asynchronous nature so that an application does not freeze while something is beeing downloaded. If that (freezing) is what you need, you must disable (by any means you see fit) your app manually and enable it only when data has been received and processed.

              I have a solution with a bool attribute in the class, which chance to true if everything is finish. But unfortunaly it needs a while(true) to handle this, and i don0t like while(true)'s in a webapplication...

              You won't be able to do that: while(true) is simply an infinite loop: Flash is not multithreaded. Only a handful of functions (like the one I've mentioned above) are really asynchronous. But Flash Player won't do anything if you put while(true) loop. It will wait until your code completes (wich, of course, will not) and only then it will give you the data that has been downloaded.

               

              Michael

              1 person found this helpful
              • 4. Re: Problem with execution order
                Flex4Education Level 1

                Thank you, That explains a lot!

                 

                I just need to Dispatch an event at some point in a AS3 class. (in a else{}).

                And i need the handler for this event in the MXML-Class in which the ASClass is called.

                 

                I think that shouldn't be a Problem, just DispatchEvent and a Eventhandler. But i'm not able to call the DispatchEvent-Method

                in the ASClass and i'm not able either, to say className.addEventListener.

                 

                Maybe i try something horrible wrong, but i don't get how to do it different right now.

                 

                thx

                 

                F4E

                • 5. Re: Problem with execution order
                  Flex4Education Level 1

                  Hi

                   

                  I think i've made some steps forward.

                   

                  I've now a class for my custom eventdispatcher, and i dispatch it in a different class like this:

                   

                  [Code]

                   

                  done.dispatchEvent(new Event('operationDone',true));

                   

                  [\Code]

                   

                  and i try to catch it like this:

                   

                  [Code]

                   

                  this.addEventListener('operationDone', doneHandler );

                   

                  [\Code]

                   

                  But the EventListener is never Called.

                  Do i have to set a specific target in the Custom Event class?

                  now i've just:

                   

                  [Code]

                   

                  public function DoneEvent(target:IEventDispatcher=null)

                  {

                       super(target);

                  }

                   

                  [\Code]

                  • 6. Re: Problem with execution order
                    rootsounds Level 4

                    You aren't using a custom event class. You have created a generic Event object. This is fine, but there is clearly some confusion.

                     

                    In simple terms, events operate based on a string key which is used to indentify them. You create custom classes that extend Event in order to encapsulate your keys as public constants and possibly to add parameters to the constructor so relevant information may be passed along to those listening.

                     

                    You may attach listeners directly to the objects that will be firing the event. To get more advanced, read up on bubbling.

                    • 7. Re: Problem with execution order
                      Anuradha@ Level 1

                      why dont u go for a progress bar..

                      show the progress bar right before the backend call and remove it in the result-handler.

                      • 8. Re: Problem with execution order
                        Flex4Education Level 1

                        Hi @ All

                         

                        A Progressbar for nothing? okey better than

                        that what i've in the moment.

                         

                        All i need is a possibility to call a method from the parent class of my as3 class.

                        Can Please! someone give a [Code] example, so that i can understand what's needed to do that!

                         

                        thx

                        F4E

                        • 9. Re: Problem with execution order
                          rtalton Level 4

                          It would be helpful if you could post the code

                          in your component which dispatches the event, and the code in the parent which is listening for the event.

                          Then it would be easy to troubleshoot. Just a guess, but either the listener is not attached to the right component, or the event is not being dispatched at all. Or maybe both.

                          • 10. Re: Problem with execution order
                            Flex4Education Level 1

                            Hi

                             

                            i can't post all the code here, but what you need to know is here:

                             

                            Code

                            //Diese Klasse errechnet das Resultat aus den Spielzügen
                            package scripts
                            {


                            import com.adobe.fiber.core.model_public;

                            import components.Erfassen.ErfassenMatch;

                            import flash.events.Event;
                            import flash.events.EventDispatcher;
                            import flash.net.URLRequest;
                            import flash.net.navigateToURL;

                            import mx.collections.ArrayCollection;
                            import mx.controls.Alert;
                            import mx.core.mx_internal;
                            import mx.rpc.events.FaultEvent;
                            import mx.rpc.events.ResultEvent;
                            import mx.rpc.soap.WebService;


                            public class MatchResult
                                {                       
                                    private var matchId:int = new int();
                                    private var moveIds:ArrayCollection = new ArrayCollection();
                                    private var moves:ArrayCollection = new ArrayCollection();
                                    private var wsMoveId:WebService = new WebService();
                                    private var wsMove:WebService = new WebService();
                                    private var scoreTeam:int = 0;
                                    private var    scoreOpponent:int = 0;
                                   
                                    //Hilfsvariablen
                                    private var i:int = new int();
                                   
                                    public function MatchResult(matchId:int)
                                    {
                                        this.matchId = matchId;
                                        getMoveId();
                                    }
                                    protected function getMoveId():void
                                    {
                                        wsMoveId.loadWSDL("***********Censured***********l");
                                        wsMoveId.addEventListener(FaultEvent.FAULT, onFault);
                                        wsMoveId.addEventListener(ResultEvent.RESULT, onResultMoves);
                                        wsMoveId.selectAllMove(matchId);
                                    }
                                    protected function onResultMoves(evt:ResultEvent):void
                                    {
                                        moveIds.addItem(evt.result);
                                        setMoves();
                                    }
                                    protected function setMoves():void
                                    {
                                        i = 0;
                                        wsMove.loadWSDL("***********Censured***********l");
                                        wsMove.addEventListener(FaultEvent.FAULT, onFault);
                                        wsMove.addEventListener(ResultEvent.RESULT, onResult);
                                        setMovesHelpFunction();
                                    }
                                    protected function setMovesHelpFunction():void
                                    {
                                        try
                                        {   
                                            if (i < moveIds[0].MoveId.length)
                                            {
                                                wsMove.selectMove(moveIds[0].MoveId[i]);
                                                i++;
                                            }
                                            else
                                            {   
                                                //--------------------- !!! Here should be called a function which is in the file, which contaisn the           -instance of this class!
                                            }
                                        }
                                        catch (err:Error)
                                        {  }
                                    }
                                    protected function onResult(evt:ResultEvent):void
                                    {
                                        if (evt.result.move_chance_id_move_chance == 3)
                                        {
                                            if (evt.result.userTeamMove == 0)
                                            {
                                                scoreTeam++;
                                            }
                                            else
                                            {
                                                scoreOpponent++;
                                            }
                                        }
                                        setMovesHelpFunction();
                                    }
                                    protected function onFault(evt:FaultEvent):void
                                    {
                                        /*if ( evt.fault.faultString != "Workflow returned empty result")
                                        {
                                            Alert.show("Es konnte keine Verbindung zum Webservice aufgebaut werden. Bitte versuchen sie es später erneut","Verbindungsfehler",Alert.OK,null,restartApplication);   
                                        }*/
                                    }
                                    protected function restartApplication(evt:Event):void {
                                        navigateToURL(new URLRequest("***********Censured***********"),"_self");
                                    }
                                    public function getResultTeam():int
                                    {
                                        return scoreTeam;
                                    }
                                    public function getResultOpponent():int
                                    {
                                        return scoreOpponent;   
                                    }

                                }
                            }
                            • 11. Re: Problem with execution order
                              Flex4Education Level 1

                              Hi

                               

                              Is it possible to Bind Class Variabels to components?

                              that would solve my problem.

                               

                              F4E

                              • 12. Re: Problem with execution order
                                MikisMM Level 2

                                I assume that what you call "Class Variable" refers to static variables and constants. The answer is yes an no. If you try to bind to a static variable, compiler will emit warning that it is unable to bind to that variable and will only capture its value when binding process is executed (component is created). If you try to change the value, binding target will not get notified and will not use the new value. As for static constants, compiler won't emit any warnings and will capture constant's value. But since constants can't be changed during runtime, it does not do any good for you either.

                                • 13. Re: Problem with execution order
                                  Flex4Education Level 1

                                  Hi

                                   

                                  This is exactly what i get.

                                   

                                  thx

                                  F4E