3 Replies Latest reply on May 21, 2009 7:42 PM by YorFlexGuy

    public property and component instantiation order problem

    YorFlexGuy

      Hello everybody.

       

      I've been trying to tackle the instantiation flow of components, and do things the right way, but I seems to be running into problems.

       

      For example, I have a custom component, with some public properties variables exposed. I create my component and set my variables like this:

      //  PARENT

      btn = new ConditionBtn();

      btn.aXML = myXML;

      btn.addEventListener("change", answerChangeHandler);
      this.addChild(btn);

       


      And inside my component, I am trying to handle the instantiation like this:

      // CHILD COMPONENT

      private var isSelected:Boolean;

       

      //pre-initialize event handler

      private function preInitialize():void{
      }

       

      //initialize event handler
      private function initialize():void{

          if(aXML.@selected == "true"){

                selected = true;

           }else{

                selected = false;

           }
      }

       

      //creation complete event handler
      private function creationComplete():void{
      }

       

       

      This doesn't work though because when teh initialize event is fired, aXML shows as NULL when i debug. This doesn't make sense to me though because the initialize event is not fired until I call this.addChild from the parent, which is done AFTER i set the aXML value in my component so one would think the data should be there already.

       

      I tried moving the logic into the creationComplete hander but aXML is still null at that point. The workaround that i'm doing is creating another function which I call manually after I add it to the stage like this:

       

      // PARENT

      btn = new ConditionBtn();

      btn.aXML = myXML;

      btn.addEventListener("change", answerChangeHandler);
      this.addChild(btn);

      btn.bindManually();

       

      // CHILD COMPONENT

      private var isSelected:Boolean;
      public function bindManually():void{

          if(aXML.@selected == "true"){

                selected = true;

           }else{

                selected = false;

           }
      }

       

       

       

      Now this works, but it seems like a really bad practice. Isn't the whole point of the pre-init and initialize events is to let you setup your component beforehand so when it finally draws itself everything is setup and doesn't have to be re-setup?

      Am i doing something wrong?