6 Replies Latest reply on Oct 13, 2008 11:40 AM by ntsiii

    Calling a method in Parent component from Title Window

    thejavafreak Level 1
      Hi all,

      I have a parent component that opens up a Title window when I click a button. Now I want to call a method in that parent component from the Title window. How do I do this in Flex? Could anyone give me a hint please.

      Thank you in advance for the help
        • 2. Re: Calling a method in Parent component from Title Window
          happybrowndog
          That's goddamned ridiculous. What were Flex developers thinking that you have to write a custom event to call back to a parent component?? Other GUI libraries such as WxWidgets, Fox, Qt, Delphi, MFC, WinForms, etc., all allow you to either call via a reference to the parent object or submit a callback function into the child object. That's just basic OO programming. Flex is looking more and more ridiculous and more like Swing - tons of unnecessary coding to do simple things.
          • 3. Re: Calling a method in Parent component from Title Window
            Level 7

            "happybrowndog" <webforumsuser@macromedia.com> wrote in message
            news:gctmql$4t5$1@forums.macromedia.com...
            > That's goddamned ridiculous. What were Flex developers thinking that you
            > have
            > to write a custom event to call back to a parent component?? Other GUI
            > libraries such as WxWidgets, Fox, Qt, Delphi, MFC, WinForms, etc., all
            > allow
            > you to either call via a reference to the parent object or submit a
            > callback
            > function into the child object. That's just basic OO programming. Flex
            > is
            > looking more and more ridiculous and more like Swing - tons of unnecessary
            > coding to do simple things.

            You absolutely _do_ have the capability to pass in a reference to the parent
            component, or to create a "hard" reference to Application.application. But
            these are not recommended practices, because anything you create this way is
            then tied to an environment that implements those properties and methods.

            Q (3): I want to run a function in my main application from inside my
            custom component. But when I try to refer to myFunction() in that
            component, I get a compile time error Call to a possibly undefined function
            myFunction. How can I fix this?

            A: Your component has its own scope, so it doesn't know anything
            about the functions in the main file. You can get around this by directly
            referencing the main application scope like this:
            Application.application.myFunction(). However, this makes your component
            tightly coupled, which is a quick way of saying that your component is only
            usable in an application that has a myFunction() function in it. You're
            better off dispatching an event from your component and letting the
            application decide how to handle it. For more information, check out the
            following resources:
            http://www.adobe.com/devnet/flex/articles/loose_coupling.html
            http://www.adobe.com/devnet/flex/articles/graduating_pt1.html



            From http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf






            • 4. Calling a method in Parent component from Title Window
              happybrowndog Level 1
              quote:

              Originally posted by: Newsgroup User

              >"happybrowndog" <webforumsuser@macromedia.com> wrote in message
              news:gctmql$4t5$1@forums.macromedia.com...
              >> That's goddamned ridiculous. What were Flex developers thinking that you
              >> have
              >> to write a custom event to call back to a parent component?? Other GUI
              >> libraries such as WxWidgets, Fox, Qt, Delphi, MFC, WinForms, etc., all
              >> allow
              >> you to either call via a reference to the parent object or submit a
              >> callback
              >> function into the child object. That's just basic OO programming. Flex
              >> is
              >> looking more and more ridiculous and more like Swing - tons of unnecessary
              >> coding to do simple things.

              >You absolutely _do_ have the capability to pass in a reference to the parent
              >component, or to create a "hard" reference to Application.application. But
              >these are not recommended practices, because anything you create this way is
              >then tied to an environment that implements those properties and methods.

              You didn't mention passing a callback function from the parent to the child, which would truly achieve the loose coupling you desire. In any case, writing a custom event also requires the parent application to be modified to know what that event is going to be and be coded for it. That's not loose coupling either.
              • 5. Re: Calling a method in Parent component from Title Window
                Level 7

                "happybrowndog" <webforumsuser@macromedia.com> wrote in message
                news:gcu3ou$j0l$1@forums.macromedia.com...
                >
                quote:

                Originally posted by: Newsgroup User
                >
                > "happybrowndog" <webforumsuser@macromedia.com> wrote in message
                > news:gctmql$4t5$1@forums.macromedia.com...
                > > That's goddamned ridiculous. What were Flex developers thinking that
                > > you
                > > have
                > > to write a custom event to call back to a parent component?? Other GUI
                > > libraries such as WxWidgets, Fox, Qt, Delphi, MFC, WinForms, etc., all
                > > allow
                > > you to either call via a reference to the parent object or submit a
                > > callback
                > > function into the child object. That's just basic OO programming. Flex
                > > is
                > > looking more and more ridiculous and more like Swing - tons of
                > > unnecessary
                > > coding to do simple things.
                >
                > You absolutely _do_ have the capability to pass in a reference to the
                > parent
                > component, or to create a "hard" reference to Application.application.
                > But
                > these are not recommended practices, because anything you create this way
                > is
                > then tied to an environment that implements those properties and methods.
                >
                > You didn't mention passing a callback function from the parent to the
                > child,
                > which would truly achieve the loose coupling you desire. In any case,
                > writing
                > a custom event also requires the parent application to be modified to know
                > what
                > that event is going to be and be coded for it. That's not loose coupling
                > either.

                The parent always has to know something about the children.


                • 6. Re: Calling a method in Parent component from Title Window
                  ntsiii Level 3
                  Further, a custom event is not required. If the component exposes the data in public properties, then any handler can access that data via the event.target reference.


                  Tracy