ListenerManager

Kind of class:public class
Package:org.casalib.events
Inherits from:Destroyable
Implements:
Version:09/06/09
Author:Aaron Clinger
Classpath:org.casalib.events.ListenerManager
File last modified:Sunday, 06 September 2009, 09:11:13
Creates an easy way to implement IRemovableEventDispatcher when you cannot extend directly from RemovableEventDispatcher.
Example:
  • package {
        import flash.events.MouseEvent;
        import org.casalib.display.CasaMovieClip;
        import org.casalib.display.CasaSprite;
        import org.casalib.events.ListenerManager;
    
    
        public class MyExample extends CasaMovieClip {
            protected var _button:CasaSprite;
            protected var _buttonListenerManager:ListenerManager;
    
    
            public function MyExample() {
                super();
    
                this._button = new CasaSprite();
                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
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

Class methods

getManager

public static function getManager (
dispatcher:IEventDispatcher) : ListenerManager

Registers a IEventDispatcher to be managed by ListenerManager.
Parameters:
eventDispatcher:
The IEventDispatcher instance 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) : void

Notifies the ListenerManager instance that a listener has been added to the IEventDispatcher.
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.

destroy

override public function destroy (
) : void

removeEventListener

public function removeEventListener (
type:String, listener:Function, useCapture:Boolean = false) : void

Notifies the ListenerManager instance that a listener has been removed from the IEventDispatcher.
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 (
) : void

Removes all event listeners.
#

removeEventsForListener

public function removeEventsForListener (
listener:Function) : void

Removes all events that report to the specified listener.
#
Parameters:
listener:
The listener function that processes the event.
#

removeEventsForType

public function removeEventsForType (
type:String) : void

Removes all events of a specific type.
#
Parameters:
type:
The type of event.
#