4 Replies Latest reply on Oct 22, 2006 3:48 AM by /usr/local/ΕΨΗΕΛΩΝ

    Calling a parent's method from a child

    /usr/local/ΕΨΗΕΛΩΝ
      Hello,
      I'm having a problem when trying to make a control that is child to my application to communicate with it by calling one of its methods.

      I have made a test environment where the main application class [classname Default] has a Web Service object in it, and a child control, dynamically loaded with SWFLoader, [classname Tester] that wants to invoke parent's web service request. For a million reasons I'm not explaining here the Web Service call MUST be done by the parent, so I won't obviously implement another identical Web Service object in the child.
      I have seen the example of using SystemManager to let parent classes alter children's properties, and I could use the same technique to let child edit properties of parent the same way, by converting this.root to SystemManager (this.parentApplication raises an exception). Reference here.

      I understand I can use SystemManager to edit parent's properties, but how can I call methods? Is it the best way?
      Thanks in advance.
        • 1. Re: Calling a parent's method from a child
          /usr/local/ΕΨΗΕΛΩΝ Level 1
          I have found the solution, and it works quite good.
          Just use events!

          I defined two event classes, one for child-parent requests, and another for parent-child response :)
          The request class has a property sender which is the component that generated the request. This is used by the parent to dispatch the response event to the correct caller.
          Both events have a property data that carries request parameters and response data in the form of Object.

          I'll post an example as soon as I can
          • 2. Re: Calling a parent's method from a child
            Level 7
            I have found the solution, and it works quite good.
            Just use events!

            I defined two event classes, one for child-parent requests, and another for
            parent-child response :)
            The request class has a property sender which is the component that
            generated the request. This is used by the parent to dispatch the response
            event to the correct caller.
            Both events have a property data that carries request parameters and
            response data in the form of Object.

            I'll post an example as soon as I can

            • 3. Re: Calling a parent's method from a child
              ntsiii Level 3
              Please do. Passing data between components is about the third question every Flex developer has.

              And while not the only way to pass data, using events is considered "best practice", because it makes your components re-usable.

              I'd really like to see a good, simple example of this.

              Tracy
              • 4. Re: Calling a parent's method from a child
                /usr/local/ΕΨΗΕΛΩΝ Level 1
                Sorry for being so late answering but my PC is broken.

                I have made a code example on parent-child communication but I found I can only make parent communicate with child, and not vice versa. This is really strange, since I used child-parent communication to ask data from the parent control in my application and there it works. The only difference is that in my code example I made the child control as an application, and not a control.

                Here is the code
                Download .zip

                The example is designed as follows:
                The main application is called the "parent" control and it dynamically loads a child control into an SWFLoader control. The loaded control is called the "child" control.
                Both controls have a text box and two buttons.
                On the parent side, after writing some text inside the text box, click "Tell Child" and the child control will display the text in its own text box. Type some text inside the child's text box and click the parent's button "Ask Child" and then the parent control will display child's text in its text box. On the child-side, the buttons work the same way the parent's: type some text, click "Tell Parent" and parent will display text. "Ask parent" will retrieve text from the parent.


                The program works partially: you can only let send text from parent to child (1 of 4 features). If someone may help me with debugging I would really appreciate it. I don't know why the code doesn't work in this example only.