5 Replies Latest reply on Dec 29, 2006 7:11 PM by peterent

    How to call a custom method in a parent container.

    Level 7
      I have an MXML component that defines the method:

      public function showAllGlyphs(spots:Array):void { ... }

      There is a child component of this MXML componet called DataClip, which has
      a method that needs to call the parent method:

      ....
      parent.showAllGlyphs(pushPins);
      ...

      I get a 1061 error when compiling. I understand the problem. The compiler
      thinks the paret is a static class, and does not know about the extention.
      My question is how to I cast or otherwise reference this method in the
      PARENT?




      Severity Description Resource In Folder Location Creation Time Id
      2 1061: Call to a possibly undefined method showAllGlyphs through a
      reference with static type flash.display:DisplayObjectContainer.
      DataClip.mxml Transitions line 32 August 21, 2006 3:07:28 PM 169


        • 1. Re: How to call a custom method in a parent container.
          peterent Level 2
          As a rule of thumb, a child should never call functions in its parent. It is possible, it just not a very good programming practice.
          If a child needs the parent to do something, the child should dispatch an event. In your child, declare an event using Metadata:

          <mx:Metadata>
          [Event("showGlyphs")]
          </mx:Metadata>

          The child should have a public property that the parent can access to handle the event:

          public var pushPins:Array;

          When the child needs the parent to do this, have the child set the pushPins array, then dispatchEvent( new Event("showGlyphs") );

          In the parent, on the child's tag, have it listen for the event and execute it:

          <MyChild showGlyphs="showAllGlyphs(event.target.pushPins)" />

          I know this seems like a lot more work, but it is a much better strategy than having children reach up and into the parent's code.
          • 2. Re: How to call a custom method in a parent container.
            Level 7
            I understand why what you say is the correct design pattern (much more
            modularity and the potential for re-use).

            In my case, it still might make sense to use .parentDocument (my problem was
            not understanding the difference between .parent and .parentDocument)
            Although I still don't understand why .parentDocument does work.

            I have a bunch of purely GUI oriented subComponents of a .parentApplication.
            The parentApplication has a custom AS-only module that does the "business
            logic, network connections, etc).

            Basically, I want a module (Singleton Class) that is accessible from all GUI
            subcomponents that provides general utility functions. It can't be
            instatiatiated multiple times. So there needs to be one global place for it.

            It does not really make things clear if everything is done via an event
            paradigm. (Well, maybe to me, but I have a Ph.D. in CS so I am tainted) I
            want this clear to the average programmer.


            --
            ===============================
            Dr. Yechezkal Gutfreund
            Team ACE for Mobile Devices
            ===============================

            "peterent" <webforumsuser@macromedia.com> wrote in message
            news:ecd85o$gmd$1@forums.macromedia.com...
            > As a rule of thumb, a child should never call functions in its parent. It
            > is
            > possible, it just not a very good programming practice.
            > If a child needs the parent to do something, the child should dispatch an
            > event. In your child, declare an event using Metadata:
            >
            > <mx:Metadata>
            > [Event("showGlyphs")]
            > </mx:Metadata>
            >
            > The child should have a public property that the parent can access to
            > handle
            > the event:
            >
            > public var pushPins:Array;
            >
            > When the child needs the parent to do this, have the child set the
            > pushPins
            > array, then dispatchEvent( new Event("showGlyphs") );
            >
            > In the parent, on the child's tag, have it listen for the event and
            > execute it:
            >
            > <MyChild showGlyphs="showAllGlyphs(event.target.pushPins)" />
            >
            > I know this seems like a lot more work, but it is a much better strategy
            > than
            > having children reach up and into the parent's code.
            >


            • 3. Re: How to call a custom method in a parent container.
              peterent Level 2
              Why didn't you say that :-)

              ActionScript is very happy to make you a singleton:

              class MyGlobal /* extends if necessary */
              {
              public function MyGlobal() { }
              static private var _self:MyGlobal;
              static public function get instance() : MyGlobal {
              if( _self == null ) _self = new MyGlobal();
              return _self;
              }
              }

              Now whenever you need it, import MyGlobal and then: MyGlobal.instance will get you the singleton. No need to reference parents or parentDocuments, etc.

              Unfortunately, you cannot have a protected or private constructor in ActionScript, so there is the danger someone will code a new MyGlobal().
              • 4. Re: How to call a custom method in a parent container.
                nicetim
                Hi,
                I have no input on your problem but i would like to ask a question that is similar in nature.

                I am using the WindowedApplication from a Flex example to open up windows.

                I have a panel (main) with a tileList (TL). the tileList has a renderer (items), when the user clicks the itemRenderer (items) it opens a new WindowedApplication window (editor).

                I need the editor to fire a refresh function on the main window.
                I have tried to use dispatchEvent but i cant seem to get the hiearchy right because the listner in main never fires.

                how can i accomplish this?

                thanks and sorry for posting this question here.
                • 5. Re: How to call a custom method in a parent container.
                  peterent Level 2
                  Event bubbling works only on events that each component in the hierarchy handles. For example, "click" is an event nearly every component handles. So if your itemRenderer were to dispatch a click event, then you could easily catch it anywhere in the hierarchy. But there isn't any event that exists to do what you want. So you need to create a custom event which involves a few steps.

                  1: in your itemRenderer code, add meta data for the event:
                  <mx:Metadata>[Event(name="refresh")]</mx:Metadata>

                  2: Have your itemRenderer dispatch the refresh event:
                  dispatchEvent(new Event("refresh"));

                  3. Create a class which extends TileList and which also has metadata for the new refresh event:
                  package {
                  [Event(name="refresh")]
                  class MyTileList extends TileList {
                  }
                  }

                  4: Use your MyTileList in place of <mx:TileList> and catch the event:
                  <MyTileList refresh="handleRefresh()" ... >