Over 10 years of community discussion and knowledge are maintained here as a read-only archive.
<?xml version="1.0" encoding="utf-8"?><mx:Box xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="80" direction="horizontal"> <mx:Metadata> [Event('click')] </mx:Metadata> <mx:Script> <![CDATA[ public static const CLICK:String = 'click'; // send the named event private function sendEvent( eventName:String ):void { dispatchEvent( new Event( eventName, true ) ); } ]]> </mx:Script> <mx:Repeater id="rp"> <mx:Image source="{rp.currentItem}" buttonMode="true" width="60" height="60" click="sendEvent( CLICK )"/> </mx:Repeater></mx:Box>
<?xml version="1.0" encoding="utf-8"?><mx:Box xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="80" direction="horizontal"> <mx:Script> <![CDATA[ // send the event up since it doesn't bubble by default private function sendEvent( event:MouseEvent):void { dispatchEvent(event); } ]]> </mx:Script> <mx:Repeater id="rp"> <mx:Image source="{rp.currentItem}" buttonMode="true" width="60" height="60" click="sendEvent(event)"/> </mx:Repeater></mx:Box>
... mediatoroverride public function onRegister():void { // assuming the component was named mediatorComponent mediatorComponent.addEventListener(MouseEvent.CLICK, mouseClickHandler, false, 0, true);}
Again, perhaps I was mislead by the example he gave and he does indeed need custom event names
override public function onRegister():void { assetPreview.addEventListener( MouseEvent.CLICK, onClick); }
The example you took from is more if you want a way of identifying which item was clicked and to dispatch custom events based on that knowledge.
package events{ import flash.events.Event;public class CustomEvent extends Event { public var yourProperty:String; public function CustomEvent (yourProperty,type:String) { //Important thing here its that has to bubbles with the second parameter set equal to true :D super(type,true,true); this.yourProperty=yourProperty; } override public function clone():Event{ return new CustomEvent (yourProperty,type); } }}
<mx:Metadata> [Event("CustomEvent",type="events.CustomEvent")]</mx:Metadata>
private function sendEvent():void{ //Instantiate the custom event var evtObj:CustomEvent=new CustomEvent(yourVarValue,"CustomEvent"); //dispatch the Event dispatchEvent(evtObj);}
override public function onRegister():void{ yourComponentReference.addEventListener("CustomEvent",handleCustomEvent); }private function handleCustomEvent(evt:CustomEvent):void{ trace(evt.yourProperty);}