5 Replies Latest reply on Nov 14, 2010 11:11 PM by Shongrunden

    Using .contains on spark elements

    JoshBeall Level 1

      Hi All,

       

      I initially tried to use addChild to add Spark Components, but that throws an error saying to use addElement instead.  Which is fine, so I've been using that (along with removeElement to remove children as necessary).

       

      I then found myself in a situation in which I wanted to know if a particular component was a child of a particular container.  I saw there was a contains method on my Spark container, but figured I would need a containsElement, similar to how I couldn't use addChild/removeChild but has to use Spark specific add/remove methods.  I thought my suspicion was confirmed when I came across this bug: spark.components.Group lacks containsElement function for operating on IVisualElement.

       

      I tried a few workarounds, none of them worked, and then just for grins I thought I would see what .contains would do if I tried it.  Well, it works.  Every container that is a child of DisplayObjectContainer has a contains method that takes a DisplayObject to look for.  All the spark components I checked are children of DisplayObject--are there any spark components that are not DisplayObject children?

       

      So, my question is--what is front with using the .contains method on the Spark containers to find Spark components that are children of the container?  Does it not work in some scenario?  It seems to be working for me--but I'm wondering if I'm missing something since this bug seems to be indicating that it's not working, and it will be fixed in a future version of Flex.

       

      Thanks!

       

        -Josh

        • 1. Re: Using .contains on spark elements
          Shongrunden Adobe Employee

          GraphicElements are not DisplayObjects (Rect, Ellipse, BitmapImage, etc.) and sometimes they won't work with the contains method.

           

          This blog post might be of use to you: http://flexponential.com/2009/12/01/spark-equivalent-of-displayobjectcontainer-contains/

           

          When you say the workarounds didn't work for you can you give an example?

          • 2. Re: Using .contains on spark elements
            JoshBeall Level 1

            If I tried something like this:

             

            container.addElement(child);

             

            Then the following would output "Container does not contain child"

             

            if(child.parent == container){

                Alert.show("Container contains child");

            } else {

                Alert.show("Container does not contain child");

            }

             

            Oddly enough, I found the statement (child.parent == container.parent) would return true... so I'm not sure what was going on.  Something about what a "parent" actually means in this case?

             

            I've read that Flexponential blog post, and reading it lead me to the bug SDK-24203, which is why I am posting here :-)

             

            It sounds to me like I will be ok to use contains so long as I am only looking for "widget" type components (e.g., buttons, dropdownlists, etc.) as opposed to GraphicElements?

            • 3. Re: Using .contains on spark elements
              Shongrunden Adobe Employee

              Is your container a SkinnableContainer?  What if you use .owner instead of .parent, does that work?

               

              owner vs. parent explanation here: http://flexponential.com/2009/12/08/differences-between-ivisualelement-parent-and-ivisuale lement-owner/

              • 4. Re: Using .contains on spark elements
                JoshBeall Level 1

                Well, I've already changed my code to use .contains--is there a reason (speed, robustness, etc.) that I should try changing it to check is element.owner == container?

                 

                In my case, all I want to do is see if a particular control has already been added to a particular container.  If it is present, in some case I might remove it.  In other cases, I might add it.

                 

                To be a little more concrete, I've got "yes/no" question that the user has to answer, and if they answer "yes" they have to provide additional information.  So, I've got a change listener on the dropdownlist, and if they change it to "yes" I insert a textbox where they can enter an explanation.  But before I call addElement I want to check if the textbox is there already or not.

                 

                If they change the answer from "yes" back to "no", then I want to remove the textbox.  But before I call removeElement I want to check to see if the textbox is present in the container or not.

                • 5. Re: Using .contains on spark elements
                  Shongrunden Adobe Employee

                  Remember that contains() acts slightly differently than element.owner == container, for example:

                   

                  <s:Group id="myGroup">

                      <s:Group>

                          <s:Group>

                              <s:Button id="myButton" />

                              ...

                   

                   

                  myGroup.contains(myButton) returns true

                  myButton.owner == myGroup returns false

                  myButton.parent == myGroup returns false

                   

                  It sounds like contains() is doing what you need, so I believe it should be ok to use it as long as you stay away from checking GraphicElements.  It's possible that in the future there might be more things that contains() won't behave well with, for example maybe a lighter weight text component, but I don't know if/when that might happen.