I would still be inclined to set an event listener in the parent...

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
     xmlns:appButtonComponent="*"
     layout="vertical"
     initialize="init();"
     verticalScrollPolicy="off"
     horizontalAlign="left">
     <mx:Script>
         <![CDATA[
             import flash.events.Event;
             import mx.controls.Alert;

                private function init():void
                {
                    addEventListener(MouseEvent.CLICK, handleMouseEvent);
                }

             private function handleMouseEvent(event:MouseEvent):void
             {
                 event.stopImmediatePropagation();
                 Alert.show("Button Clicked", event.target.label);
             }
         ]]>
     </mx:Script>
     <appButtonComponent:ButtonComponent id="myButtonComponent"/>
</mx:Application>


--- In [email protected], Angelo Anolin <angelo_ano...@...>
wrote:
>
> Hi Valdhor,
>
> Thanks for the advice.
>
> I have created a sample button component and a parent component to
implement this.
>
> Parent.mxml
> <?xml version="1.0" encoding="utf-8"?>
> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml";
> Â Â Â Â xmlns:appButtonComponent="*"
> Â Â Â Â layout="vertical"
> Â Â Â Â initialize="init();"
> Â Â Â Â verticalScrollPolicy="off"
> Â Â Â Â horizontalAlign="left">
> Â <mx:Script>
> Â Â <![CDATA[Â
> Â Â Â import flash.events.Event;
> Â Â Â
> Â Â Â private function myButtonComponent_Click(evt:Event)
:void
> Â Â Â {
> Â Â Â Â Alert.show("Button Clicked", evt.target.label);
> Â Â Â }
> Â Â Â ]]>
> Â </mx:Script>
> Â
> Â <appButtonComponent:ButtonComponent id="myButtonComponent"
click="myButtonComponent_Click(event)"/>
> </mx:Application>Â
>
>
> ButtonComponent.mxml
> <?xml version="1.0" encoding="utf-8"?>
> <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml";
verticalAlign="middle" horizontalAlign="left" horizontalGap="2"
height="50">Â
> Â <mx:Script>
> Â Â <![CDATA[
> Â Â ]]>
> Â </mx:Script>
> Â
> Â <mx:Button id="btn1" label="Button 1" width="100" />
> Â <mx:Button id="btn2" label="Button 2" width="100" />
> </mx:HBox>Â
>
> Clicking on any button on the component will display the label of the
button which was clicked.
>
> I wonder if I am doing things the "RIGHT" way through this? Although I
believe I achieved what I wanted to do through this means. I would only
then use a switch statement and process the necessary method or function
based on the button which was clicked.
>
> Any further advice and better methods of coding would be highly
appreciated.
>
> Thanks,
>
> Angelo
>
>
>
>
> ________________________________
> From: valdhor valdhorli...@...
> To: [email protected]
> Sent: Thu, 15 April, 2010 13:48:04
> Subject: [flexcoders] Re: MXML Component Call Event in Parent
>
> Â
> I don't really have any simple examples.
>
> I did look at the documentation for the Button Class (http://livedocs.
adobe.com/ flex/3/langref/ flash/display/ InteractiveObjec t.html#event:
click) and noted the bubbles property is set to true so the button click
should bubble up to your parent component.
>
> So, add an event listener in the parent component to catch the event.
If you have multiple buttons, you will have to check the event.target
property to find out which button dispatched the event (hint: Add a
breakpoint in your handler and check the event.target and
event.currenttarget properties).
>
> --- In flexcod...@yahoogro ups.com, Angelo Anolin <angelo_anolin@ ...>
wrote:
> >
> > Hi Valdhor,
> >
> > Care to show some examples? Or some links? Thanks.
> >
> > Angelo
> >
> >
> >
> >
> > ____________ _________ _________ __
> > From: valdhor <valdhorlists@ ...>
> > To: flexcod...@yahoogro ups.com
> > Sent: Thu, 15 April, 2010 12:14:11
> > Subject: [flexcoders] Re: MXML Component Call Event in Parent
> >
> > ÂÂ
> > Make sure the event is set to bubble. Once it has, the event should
bubble up the chain to the parent where an event listener should catch
it.
> >
> > --- In flexcod...@yahoogro ups.com, Angelo Anolin <angelo_anolin@
...> wrote:
> > >
> > > Hi FlexCoders,
> > >
> > > I have created a component consisting of some buttons which I am
using in another MXML component.
> > >
> > > How would I trigger from the parent component an event which was
done inside the component.  For example, I clicked on a button on
the component which I embedded, that should map to an event on the
parent containing the component.
> > >
> > > I am looking to reuse the same component in other MXML files as
well.
> > >
> > > Thanks. Appreciate your inputs.
> > >
> > > Regards,
> > >
> > > Angelo
> > >
> >
>

Reply via email to