ListenerManager
| Kind of class: | public class |
|---|---|
| Package: | org.casalib.events |
| Inherits from: | Destroyable |
| Implements: | |
| Version: | 11/10/08 |
| Author: | Aaron Clinger |
| Classpath: | org.casalib.events.ListenerManager |
| File last modified: | Monday, 01 December 2008, 11:16:36 |
Creates an easy way to implement IRemovableEventDispatcher when you cannot extend directly from RemovableEventDispatcher.
Example:
-
package { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.MouseEvent; import org.casalib.events.ListenerManager; public class MyExample extends MovieClip { protected var _button:Sprite; protected var _buttonListenerManager:ListenerManager; public function MyExample() { super(); this._button = new Sprite(); this._button.graphics.beginFill(0x00FF00); this._button.graphics.drawRect(0, 0, 150, 150); this._button.graphics.endFill(); this.addChild(this._button); this._buttonListenerManager = ListenerManager.getManager(this._button); this._buttonListenerManager.addEventListener(MouseEvent.MOUSE_OVER, this._onMouseOver); this._button.addEventListener(MouseEvent.MOUSE_OVER, this._onMouseOver); this._buttonListenerManager.addEventListener(MouseEvent.MOUSE_OUT, this._onMouseOut); this._button.addEventListener(MouseEvent.MOUSE_OUT, this._onMouseOut); this._buttonListenerManager.addEventListener(MouseEvent.CLICK, this._onClick); this._button.addEventListener(MouseEvent.CLICK, this._onClick); } protected function _onMouseOver(e:MouseEvent):void { trace("On mouse over."); } protected function _onMouseOut(e:MouseEvent):void { trace("On mouse out."); } protected function _onClick(e:MouseEvent):void { trace("On mouse clicked. No more events will fire."); this._buttonListenerManager.removeEventListeners(); } } }
To implement IRemovableEventDispatcher with out the option to extend from RemovableEventDispatcher you can use ListenerManager in this way:
package { import flash.display.Sprite; import org.casalib.core.IDestroyable; import org.casalib.events.IRemovableEventDispatcher; import org.casalib.events.ListenerManager; public class RemovableSprite extends Sprite implements IRemovableEventDispatcher, IDestroyable { protected var _listenerManager:ListenerManager; protected var _isDestroyed:Boolean; public function RemovableSprite() { super(); this._listenerManager = ListenerManager.getManager(this); } override public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { super.addEventListener(type, listener, useCapture, priority, useWeakReference); this._listenerManager.addEventListener(type, listener, useCapture, priority, useWeakReference); } override public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void { super.removeEventListener(type, listener, useCapture); this._listenerManager.removeEventListener(type, listener, useCapture); } public function removeEventsForType(type:String):void { this._listenerManager.removeEventsForType(type); } public function removeEventsForListener(listener:Function):void { this._listenerManager.removeEventsForListener(listener); } public function removeEventListeners():void { this._listenerManager.removeEventListeners(); } public function get destroyed():Boolean { return this._isDestroyed; } public function destroy():void { this.removeEventListeners(); this._listenerManager.destroy(); this._isDestroyed = true; } } }
Summary
Instance properties
Instance properties inherited from Destroyable
Class methods
- getManager (dispatcher:IEventDispatcher) : ListenerManager
- Registers a IEventDispatcher to be managed by ListenerManager.
Instance methods
- addEventListener (type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false) : void
- Notifies the ListenerManager instance that a listener has been added to the IEventDispatcher.
- removeEventListener (type:String, listener:Function, useCapture:Boolean = false) : void
- Notifies the ListenerManager instance that a listener has been removed from the IEventDispatcher.
- removeEventsForType (type:String) : void
- Removes all events of a specific type.
- removeEventsForListener (listener:Function) : void
- Removes all events that report to the specified listener.
- removeEventListeners : void
- Removes all event listeners.
- destroy : void
Instance methods inherited from Destroyable
- removeEventListeners : void
Class methods
getManager
Registers aIEventDispatcherto be managed by ListenerManager.Parameters:eventDispatcher:TheIEventDispatcherinstance to manage.Returns:- A ListenerManager instance.
Instance methods
addEventListener
public function addEventListener (type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false) : voidNotifies the ListenerManager instance that a listener has been added to theIEventDispatcher.Parameters:type :The type of event.listener :The listener function that processes the event.useCapture :Determines whether the listener works in the capture phase or the target and bubbling phases.priority :The priority level of the event listener.useWeakReference:Determines whether the reference to the listener is strong or weak.removeEventListener
public function removeEventListener (type:String, listener:Function, useCapture:Boolean = false) : voidNotifies the ListenerManager instance that a listener has been removed from theIEventDispatcher.Parameters:type :The type of event.listener :The listener function that processes the event.useCapture:Determines whether the listener works in the capture phase or the target and bubbling phases.removeEventListeners
public function removeEventListeners () : voidRemoves all event listeners.#Specified by:removeEventsForListener
public function removeEventsForListener (listener:Function) : voidRemoves all events that report to the specified listener.#Parameters:listener:The listener function that processes the event.#Specified by: - removeEventsForListener (listener:Function) : void