How do I listen for event in a more actionscript-friendly way?
I have a flex application where the child dispatches an event inside a parent:
child:
<fx:Metadata>
[Event(name="myEvent", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
dispatchEvent(new Event("myEvent"));
]]>
</fx:Script>
parent:
<comp:poppy id="myPopup" myEvent="myEventStuff(event);" />
I've changed my code so that I create myChild in actionscript, rather than the mxml tag as shown. How do I add the event liste开发者_运维知识库ner in actionscript?
public function showPoppy(evt:MouseEvent):void {
var myPopup:poppy = PopUpManager.createPopUp(this, poppy, true) as poppy;
PopUpManager.centerPopUp(myPopup:poppy );
myPopup:poppy.includeInLayout = true;
myPopup:poppy.visible = true;
// how do I add the event listener?
}
One way you could do this is break out the "myEvent" string in your poppy class to a static String within the poppy class:
public static const MY_EVENT:String = "myEvent";
Then, your ActionScript component can listen for this event from your "myPopup" instance:
myPopup.addEventListener(poppy.MY_EVENT, handleMyEvent);
And just a detail, you shouldn't put some bare code in . Prefer something like that:
<s:Group ... creationComplete="onCreationComplete">
<fx:Metadata>
[Event(name="myEvent", type="flash.events.Event")]
</fx:Metadata>
<fx:Script>
<![CDATA[
private function onCreationComplete(e:Event):void
{
dispatchEvent(new Event("myEvent"));
}
]]>
</fx:Script>
精彩评论