I searched on the Internet and did not find clues on how to do this, any idea? I don't want flex to only flash (my objects are not visual components).
Here is a very simple and incomplete custom event class that supports bubbling.You will also notice that you added an extra parameter to the object so that I can pass data.Also note that special event classes require a cloning method if the event of the dispatched event is resubmitted.For example, if the callback function from a custom event resends the event, the clone method will make a copy of the event for the new dispatcher.I also want to add that I never had to use the bubbling aspect of this class, so it is not needed.
// ActionScript file package events{ import flash.events.Event; public class DynamicEvent extends Event{ public var data:Object; private var _type:String; private var _bubbles:Boolean; private var _cancelable:Boolean; public function DynamicEvent( oData:Object, sType:String, bBubbles:Boolean = false, bCancelable:Boolean = false):void{ super(sType, bBubbles, bCancelable); this.data = oData; this._type = sType; this._bubbles = bBubbles; this._cancelable = bCancelable; } public override function clone():Event{ return new DynamicEvent(this.data, this._type, this._bubbles, this._cancelable ); } public override function get type():String{ return this._type; } public function set type( sType:String ):void{ this._type = sType; } public override function get bubbles():Boolean{ return this._bubbles; } public function set bubbles( bBubbles:Boolean ):void{ this._bubbles = bBubbles; } public override function get cancelable():Boolean{ return this._cancelable; } public function set cancelable( bCancelable:Boolean ):void{ this._cancelable = bCancelable; } } }
[EDIT]
//usage to dispatch var e:DynamicEvent = new DynamicEvent( {anyAttributeYouWantToCallIt:"someDataHere"}, "YourEventNameHere" ); this.dispatchEvent(e); // usage to listen ObjectDispatchingCustomEvent.addEventListener( 'YourEventNameHere, callBackFunc' ) //call back function public function callBackFunc( e:DynamicEvent ):void{ trace( e.data.anyAttributeYouWantToCallIt ) // will show someDataHere }
bubbling DisplayObject, .
DisplayObject
( ). , , , , . , "foo", , .
, IEventDispatcher . , , , , , ( ). Heck, , ! , , , .
IEventDispatcher
. . , MouseEvents. MouseEvent , - ( , ). root DisplayObject, DisplayObject, root.
root
, . , , , , .
, . , . .. - :
package { import flash.events.EventDispatcher; public class EventHub { public static const dispatcher : EventDispatcher = new EventDispatcher(); } }
Then, all objects (displayed and not displayed) in the application that need a notification can work through EventHub.dispatcher:
EventHub.dispatcher.addEventListener(MyCustomEvent.TYPE, myHandlerFunction); EventHub.dispatcher.dispatchEvent(new MyCustomEvent(MyCustomEvent.TYPE, ...));