7 Replies Latest reply on May 5, 2009 2:07 PM by Flex harUI

    Getting parent object reference while creating class (like MovieClips)

    janoschii

      Hi folks,

       

      i am searching for a way to get the parent object while creating.

      If i use MovieClips i have a reference to the parent object.

      So i hope there is a variable like "caller" or something

      while a class is instanciated.

       

      At the moment i have to do this:

       

      <my:MyClass parent="{this}"/>

       

      But i want to get this:

       

      <my:MyClass />

       

      Do you see any solution?

       

      ty janoschii

        • 1. Re: Getting parent object reference while creating class (like MovieClips)
          ATIF FAROOQ Level 3

          hi,

             i am not clear about your question. is that what you want

           

          <my:MyClass  creationComplete="{this.addChild(myId)}" id="myId">//now

          this[Object] will be the parent of MyClass

           

          altough you can use this method in onther events not specifically in

          creationComplete like if you instantiate your class by actionScript then you

          can set your parent Object at that time too by using addChild method.

          • 2. Re: Getting parent object reference while creating class (like MovieClips)
            janoschii Level 1

            ty for reply, but no i mean something different.

             

            1) How it is:

            If i use this in my mxml:

            <my:MyClass  parent="{this}" />

            then i can access the mxml by the variable parent.

             

            1) What I hope to can:

            I would like to have the possebility that i can only use:

            <my:MyClass  />

            without defining the parent Object.

             

            If MyClass would be an extended Class of a Canvas

            it would have automaticly the parent variable with the reference to the mxml.

            But I search for a way to get it with an normal class.

            Maybe there is a variable that i can use in the construction function?

            Or an other way?

             

            ty janoschii

            • 3. Re: Getting parent object reference while creating class (like MovieClips)
              Flex harUI Adobe Employee

              In Flash, objects placed on the timeline have a parent at constructor time.  This is not true anywhere else in the Actionscript world.  In Flex, UI objects have a lifecycle.  Their createChildren method will be called when they are added to a parent.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

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

              • 4. Re: Getting parent object reference while creating class (like MovieClips)
                janoschii Level 1

                ty for answer, but i am not sure you was reading my last reply.

                 

                I am asking myself if there is a way to handle it like UIComponents(link).

                Maybe there is a way with classes to while they instantiated.

                 

                Something like "arguments.callee" but for classes.

                A reference to the object where the class is instantiated.

                 

                janoschii

                • 5. Re: Getting parent object reference while creating class (like MovieClips)
                  Flex harUI Adobe Employee

                  At the time your UIComponent's constructor is called, the parent isn't assigned yet because the object is not on a display list.  It is different in Flash for placed objects.

                   

                  Also, in MXML, there is a document context which is the object instantiation the class and that is different than the object's parent.  In the below sample, the Sample instance (which is a subclass of the top tag VBox) is the context for instantiation of both the child HBox and the grandchild Label in the sense that, when creationComplete gets called for the HBox and Label, the 'this' pointer is actually the Sample instance (which is the document property of the HBox and Label), but the parent of the HBox is Sample and parent of Label is HBox.

                   

                  And like I said, there is a lifecycle which is worth learning.  The Flash place object lifecycle just won't work here.  There is no way from actionscript to create an instance with a parent, that is underlying Flash Player magic only available to Flash authored SWFs

                   

                  In the Flex lifecycle, you defer most of the init time work to createChildren() or commitProperties().

                  1 person found this helpful
                  • 6. Re: Getting parent object reference while creating class (like MovieClips)
                    janoschii Level 1

                    I believe I understand.

                     

                    So the only way is:

                     

                    <my:MyClass  parent="{this}" />

                     

                    Thank you so much for your help.

                     

                    Janoschii

                    • 7. Re: Getting parent object reference while creating class (like MovieClips)
                      Flex harUI Adobe Employee

                      Well, to be honest, I don't recommend that either.  While that will work for simple configurations with UIComponents, it won't work in the general case.  I would suspect that if your class is nested inside other containers it won't work or you'll have some weird bugs later.  You would find that it wouldn't work in Flash either if you created your instances via ActionScript instead of placing them on the stage or in symbols.

                       

                      There is a specific sequence of method calls involving createChildren and commitProperties that you should use instead.  Please see the documentation on those methods and on creating custom components.

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

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