5 Replies Latest reply on Nov 12, 2008 4:13 PM by ntsiii

    Order of function calls

    jjrosen1126
      So in my project I have a component with a function called init(); It was in the creation complete, but it had to wait for the main application's init function to do something first, so I removed it from there. I had the init function for the main application call this sub-component's init at the end. For some reason, the sub-component init function gets called first, which I confirmed with breakpoints and alert calls. Is this supposed to happen, or should the order of operations I expect from other languages happen?
        • 1. Re: Order of function calls
          Level 7

          "jjrosen1126" <webforumsuser@macromedia.com> wrote in message
          news:gf2b1p$fgb$1@forums.macromedia.com...
          > So in my project I have a component with a function called init(); It was
          > in
          > the creation complete, but it had to wait for the main application's init
          > function to do something first, so I removed it from there. I had the
          > init
          > function for the main application call this sub-component's init at the
          > end.
          > For some reason, the sub-component init function gets called first, which
          > I
          > confirmed with breakpoints and alert calls. Is this supposed to happen,
          > or
          > should the order of operations I expect from other languages happen?

          Could you be more specific about what you're trying to accomplish? I
          suspect your chosen approach might not be the most efficient one.


          • 2. Re: Order of function calls
            jjrosen1126 Level 1
            Basically, the main application has to grab an ID number from the address line that the chatting sub-component uses to grab that specific chat history. So I have the main application grab the ID and set it, then have it call the chat's init function. The thing that's strange is if I do the following code, it first pops up 'Chat Init' then another that says 'Main Init' Shouldn't it be the other way around? I was thinking I could probably pass the ID variable in the function call instead of in component declaration in the mxml. But if the sub-component goes first, wouldn't I still have the same problem?


            Main App file // with a call to init in the creation complete
            private function init()
            {
            Show.Alert('Main Init');
            Chat.init(); //there's a sub-component with ID Chat
            }

            Chat component // no init call in this file.
            public function init()
            {
            Alert.show('Chat Init');
            }
            • 3. Re: Order of function calls
              Level 7

              "jjrosen1126" <webforumsuser@macromedia.com> wrote in message
              news:gf45m0$t0u$1@forums.macromedia.com...
              > Basically, the main application has to grab an ID number from the address
              > line
              > that the chatting sub-component uses to grab that specific chat history.
              > So I
              > have the main application grab the ID and set it, then have it call the
              > chat's
              > init function. The thing that's strange is if I do the following code, it
              > first pops up 'Chat Init' then another that says 'Main Init' Shouldn't it
              > be
              > the other way around? I was thinking I could probably pass the ID
              > variable in
              > the function call instead of in component declaration in the mxml. But if
              > the
              > sub-component goes first, wouldn't I still have the same problem?
              >
              >
              > Main App file // with a call to init in the creation complete
              > private function init()
              > {
              > Show.Alert('Main Init');
              > Chat.init(); //there's a sub-component with ID Chat
              > }
              >
              > Chat component // no init call in this file.
              > public function init()
              > {
              > Alert.show('Chat Init');
              > }
              >

              http://www.cflex.net/showfaq.cfm

              The way I'd do it is like this:

              private var _id:int;
              private var _idChanged:Boolean;

              public function set id(num:int):void {
              if (num != NaN && num !=_id ) {
              _id = num;
              _idChanged = true;
              invalidateProperties();
              }
              }

              override protected function commitProperties(){
              super.commitProperties();
              if (_idChanged) {
              //do your logic to get info here
              }
              }

              HTH;

              Amy


              • 4. Re: Order of function calls
                chris.huston.t10 Level 3
                I think what is happening is that your Main Init is actually being displayed first, but the Chat Init is being displayed on top of the Main Init so it looks like Chat Init comes first. What happens when you use the trace function and look in the debugger for the order of execution?

                trace('main init');

                Vygo
                1 person found this helpful
                • 5. Re: Order of function calls
                  ntsiii Level 3
                  Children are instantiated first.

                  Application.creationComplete will fire after all children are rendered.

                  Tracy