12 Replies Latest reply on Jun 14, 2009 12:47 PM by mraak

    instantiating MXML class in AS3

    rickbsgu

      I have a simple MXML component:

       

      -- code -- 'MyClass'

      <mx:Canvas ... creationPolicy="none" ... >

        <mx:HBox id="innerPanel" >

          <mx:Canvas ... />

        </mx:HBox>

      </mx:Canvas>

      -- /code --

       

      I'm trying to instantiate this in AS3 as so:

       

      -- code --

      var myClass:MyClass = new myClass();

      myClass.createComponentsFromDescriptors(true);

      -- /code --

       

      myClass is created, but no children.  myClass.childDescriptors is empty.

       

      What am I doing wrong?

       

      thanx,

      rickb

        • 1. Re: instantiating MXML class in AS3
          dzeikei

          try checking for child in the handler of myClass.addEventListener(FlexEvent.CREATION_COMPLETE, myClassReadyHandler);

          1 person found this helpful
          • 2. Re: instantiating MXML class in AS3
            rickbsgu Level 1

            That would probably work, but that means I have to store things away someplace for later processing.

             

            Can do that, but it's awkward.  Would rather be able to instantiate, initialize to information, and continue on in one go.

             

            This is a simple component - I'm thinking it would be better to do it all in AS3.

             

            Thanks for the response,

            rickb

            • 3. Re: instantiating MXML class in AS3
              rickbsgu Level 1

              Actually, in thinking about this further, that would beg the question, "what is 'createChildrenFromDescriptors()' good for, if it doesn't do what it's advertised to do?' (complete the instantiation of the component immediately...)

              • 4. Re: instantiating MXML class in AS3
                dzeikei Level 2

                Hmm true, I haven't tried this, but try a simple callLater(). Flex might not be creating the children until the next rendering phase as Flex loves to defer things for efficiency.

                I have a feeling you might still have to listen for a creationComplete event on the parent to be sure, since that's exactly how you would do it in MXML to access the children as well.

                • 5. Re: instantiating MXML class in AS3
                  rickbsgu Level 1

                  Interesting mechanism...  I'll have to think about how that might be used...

                   

                  In the interim, I've restructured things a bit, using more 'Flex-ish' constructs like Repeater wth a Dataprovider that gets loaded from a variable set into the class at instantiation.  Another interesting mechanism.

                   

                  That all seems to work reasonably well - (I was hoping to not require the component to carry the data array around, but the array items are being held by the UI items, anyway, so I guess it's not that big a deal.)

                   

                   

                  Problem I'm having now is an effect that I'm applying when the panel is added to the stage:  It doesn't happen the first time, but it happens fine on subsequent calls.  Suggests the child panes loaded by the repeater object still aren't loaded.

                   

                  Still working it out...  Being able to block on a call until everything completed for instantiation (aka threading) would be a nice feature...

                   

                  Thanks,

                  rickb

                  • 6. Re: instantiating MXML class in AS3
                    Flex harUI Adobe Employee

                    A showeffect won't work because there is no "show" when an object is created.  Hook up to creationComplete as well.

                     

                    Also, Flex "bakes" the application off stage and adds everything at once at applicationComplete so when your addedToStage fires might be different at startup

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

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

                    1 person found this helpful
                    • 7. Re: instantiating MXML class in AS3
                      Flex harUI Adobe Employee

                      BTW, In the original post, the issue may be that you need to call addChild() and maybe validateNow() before calling createComponentsFromDescriptor.  The lifecycle rules are that children aren't created until you are parented.

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

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

                      • 8. Re: instantiating MXML class in AS3
                        dzeikei Level 2

                        I had a look at the issue myself instead of throwing theories at you, since you seem keen to find out why this is happening :

                         

                        var myClass:MyClass = new myClass();
                        this.addChild(myClass);
                        myClass.createComponentsFromDescriptors(true);
                        

                         

                        if you don't want to add it to the display list at the time of creation, do:

                        var myClass:MyClass = new myClass();
                        myClass.initialize();
                        myClass.createComponentsFromDescriptors(true);
                        

                         

                        This is because MXML components will not behave normally when they haven't been initialized. Initialization process creates the children, or their descriptors if the creationPolicy is set to none. createComponentsFromDescriptors() will not create any children when there are no descriptors but I believe it flags the children as having been created and you end up never being able to create any children.

                        • 9. Re: instantiating MXML class in AS3
                          rickbsgu Level 1

                          All very informative posts - thanks to all.  Some followup:

                           

                          " The lifecycle rules are that children aren't created until you are parented."

                           

                          My first impression is, 'ick' - that's a real pain, if true.  It means you can't instantiate a component, set it up the way you want (or let it set itself up the way it wants), wait for it to complete, and then add it to the stage.  Deferring all of that may make sense for the static portions of the project, but not for the dynamic portions, IMHO.

                           

                          dzeikei:

                          The 'initialize()' call sounds like a good thing to try - I'll give it a go.  That would be great, if it works.  I did try 'validateNow()', but on the component in question, and not after adding it to something - I'd rather not add it to the stage until it's set up.

                           

                          Setting as much up as possible in MXML seems to be more in line with what the toolkit expects, though (I'm used to cranking out a lot of procedural stuff to get things together.)  I'm getting a better handle on that.

                           

                          "A showeffect won't work because there is no "show" when an object is created.  Hook up to creationComplete as well."

                           

                          Ok, I'll give it a go.

                           

                          Thanks all

                          • 10. Re: instantiating MXML class in AS3
                            Flex harUI Adobe Employee

                            The main reason we wait for parenting is that it often affects the style computation that you don't want to go through twice for performance reasons.

                             

                            Alex Harui

                            Flex SDK Developer

                            Adobe Systems Inc.

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

                            • 11. Re: instantiating MXML class in AS3
                              mraak Level 1

                              Unfortunatelly nothing of the above works when you have a chain of MXML's and AS's that are instantiating eachother. Not even the combination of all of the techniques mentioned earlier.

                               

                              I tried all of the above on the entire chain, and even placed callLater in the main canvas container but still creationComplete in MyMXML happens after callLater.

                               

                              I tried to be creative with the bellow pseudo pseudo code, I hope you get the idea.

                               

                               

                              <MyMainCanvas>

                              <script>

                               

                              onCreationComplete

                              {

                              var myClass = new MyClass();

                              addChild(myClass)

                               

                              callLater(doSomething)

                              }

                               

                              doSomething()

                              {

                              // does nothing as none of the properties are available from MyMXML

                              }

                              </script>

                              </MyMainCanvas>

                               

                               

                               

                              MyClass extends Canvas

                              {

                              function MyClass()

                              {

                                var myMXML = new MyMXML();

                                addChild(myMXML);

                              }

                              }

                               

                              <MyMXML>

                              <script>

                              onCreationComplete

                              {

                                 // this gets called after callLater in MyMainCanvas

                              }

                              </script>

                              </MyMXML>

                              • 12. Re: instantiating MXML class in AS3
                                mraak Level 1

                                L&B, few moments later.

                                 

                                 

                                validateNow() seem to do the trick in my case, and I don't even need to do callLater();

                                 

                                 

                                But BEWARE, calling the method from wrong place and in a wrong sequence may mess up things even more. For me it worked when I placed it like this:

                                 

                                var myClass = new MyClass();

                                addChild(myClass)

                                myClass.validateNow()