3 Replies Latest reply on May 8, 2010 7:18 PM by ChrisInCambo2

    Flex: Method doesn’t work when being called on parentDocument

    ChrisInCambo2

      Hi,

      I wonder is anyone can look at this code and tell me why calling the  removeSelectedChild works when called from the same document, but  returns the following error when called from the child  document/component.

       

      "ArgumentError: Error #2025: The supplied DisplayObject must be a  child of the caller."

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >

        <mx:Accordion id="myAccordion"
        width="100%" height="100%"
        selectedIndex="0">

        <mx:Script>
          <![CDATA[

            public function removeSelectedChild():void {
              myAccordion.removeChild(myAccordion.selectedChild);
            }

          ]]>

        </mx:Script>

        <mx:headerRenderer>
          <mx:Component>
            <mx:Button click="{ parentDocument.removeSelectedChild() }" />
          </mx:Component>
        </mx:headerRenderer>

        <mx:HBox>
          <mx:Button click="{ removeSelectedChild() }" />
        </mx:HBox>

        </mx:Accordion>

      </mx:Application>

      Clicking on the button in the child produces the expected result,  whilst clicking on the header throws an error despite the fact they both  call exactly the same method.

      Sorry that the example is a little contrived, this problem arose in a  quite complicated view, which was using all kinds of custom components.  This was the only way I could display it in a way that will be quick  for you to compile and easy to focus on the real issue without  background noise.

      I'm pulling my hair out on this one and would really appreciate it if  anyone could help.

       

      Cheers,

       

      Chris

       


        • 1. Re: Flex: Method doesn’t work when being called on parentDocument
          ChrisInCambo2 Level 1

          UPDATE: I have now discovered that the exception is fired after the  method has finished executing. See the trace statements below. "after"  is traced before the exception is thrown.

           

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >

           

                <mx:Accordion id="myAccordion"
                width="100%" height="100%"
                selectedIndex="0">

           

                <mx:Script>
                  <![CDATA[

           

                    public function removeSelectedChild():void {
                        trace("before");
                    try {
                            myAccordion.selectedChild.parent.removeChild(myAccordion.selectedChild)
                    } catch(err:ReferenceError) {
                        trace("catch")
                        }
                        trace("after");
                    }

           

                  ]]>

           

                </mx:Script>

           

                <mx:headerRenderer>
                  <mx:Component>
                    <mx:Button click="{ parentDocument.removeSelectedChild() }" />
                  </mx:Component>
                </mx:headerRenderer>

           

                <mx:HBox>
                  <mx:Button click="{ removeSelectedChild() }" />
                </mx:HBox>

           

                </mx:Accordion>

           

              </mx:Application>

          • 2. Re: Flex: Method doesn’t work when being called on parentDocument
            Gregory Lafrance Level 6

            What are you trying to remove?

             

            myAccordion.removeChild(myAccordion.selectedChild);

            tries to remove the container, which is the child of the Accordion. Is that what you want?

             

            Also, I'm wondering if  parentDocument.removeSelectedChild() would try to remove the Accordion.

             

            If this post answers your question or helps, please mark it as such.


            Greg Lafrance - Flex 2 and 3 ACE certified

            www.ChikaraDev.com

            Flex Training and Support Services

            • 3. Re: Flex: Method doesn’t work when being called on parentDocument
              ChrisInCambo2 Level 1

              Yes I want to remove the child of the accordion. Like I said this is a very contrived example, the real app has a Repeater feeding the Accordian and is using flexlibs CanvasButtonAccodianHeader element to add some controls to each accordion header, one of which is a remove button which removes that child from the accordion. Posting all of that code would have involved several files and be difficuly to read. So I distilled the behaviour I'm looking to solve in the above code.

               

              The point is that the call to removeChild works regardless of whether it's called from an event in the child or an event in the header, the difference is that when it's called from the header it also throws an exception.