| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
The
EventBroadcaster class is the cornerstone of
the Lowra event system. The main reason which explains the presence
of this object whereas the existence of the native
EventDispatcher
class is the lack of flexibility of that class :
- It's not possible to add an object as listener for many events in a single call.
- It's not possible to force the presence of specific function in a listener object.
- It's not possible to reuse an event previously dispatched without having to create
a custom event object which overrides the
clone function.
With the EventBroadcaster class it's possible to work with many kinds
of listeners, as listed below :
- Method closure, or delegate method. You simply have to pass a reference to the
function in function for register/unregister listeners, then that method is called
with the event object as argument.
- Objects implementing specific listener interface (as with Swing in java), in that
case the function with the same name that the event type is called with the event
object as argument.
- An object which implement the
handleEvent function, in that case,
if the object doesn't have a function with a name corresponding to the event type
the handleEvent function is called with the event object as argument.
The Lowra's event broadcaster also offer many methods to dispatch event from
different type of events object. More formally, it's possible to dispatch
an event object, or an anonymous object which will be converted into a
DynBasicEvent before the broadcast, all of its properties are
then copied into this event object.
Another big difference with the native dispatcher is the fact that listeners
are systematically stored using weak references, restricting the usage of
anonymous listeners which can't be unregistered due to the lack of reference
to it.
Composition is privileged over inheritance by the use of a source object
parameter for the broadcaster. That property, if set, result in that all
event objects dispatched by the EventBroadcaster which have
their target set to null will have that object
as source instead of the dispatcher itself.
Optionally the EventBroadcaster can restrict the type
of listeners to a specific type, in that case the broadcaster only
accept the registration of objects which implements or extends the
specified Class.
protected var _cType:Class
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _mAll:Collection
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _mDelegate:HashMap
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _mEventListener:HashMap
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _mType:HashMap
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
protected var _oSource:Object
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
public function EventBroadcaster(source:Object = null, type:Class = null)
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Creates an new EventBroadcaster object with the passed-in
source object as source for events. If the source
parameter is omitted the source of events will be this event broadcaster.
Optionnaly the type of listeners objects can be restricted, in that
case you just have to pass the class of listener in the type
parameter.
Parameters
| source:Object (default = null) — an object used as target instead of this event broadcaster
for event object which have a null target
|
| |
| type:Class (default = null) — an optional class for listener
|
protected function _broadcastEvent(c:Collection, e:Event):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Broadcast the passed-in event to the listeners contained in the passed-in
Collection.
Parameters
| c:Collection — Collection of listeners to which send the event
|
| |
| e:Event — event to broadcast to listeners
|
Throws
| — UnsupportedOperationException — If one listener is an object
which have neither a function with the same name than the event type nor
a function called handleEvent
|
public function addEventListener(type:String, listener:Object, ... rest):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Adds an event listener for the specified event type.
There is two behaviors for the addEventListener
function :
- The passed-in listener is an object :
The object is added as listener only for the specified event, the object must
have a function with the same name than
type or at least a
handleEvent function.
- The passed-in listener is a function :
A
Delegate object is created and then
added as listener for the event type. There is no restriction on the name of
the function. If the rest is not empty, all elements in it is
used as additional arguments into the delegate object.
Parameters
| type:String — name of the event for which register the listener
|
| |
| listener:Object — object or function which will receive this event
|
| |
| ... rest — additional arguments for the function listener
|
Returns
| Boolean — true if the function have been succesfully added as
listener fot the passed-in event
|
Throws
| — UnsupportedOperationException — If the listener is an object
which have neither a function with the same name than the event type nor
a function called handleEvent
|
public function addListener(listener:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Adds the passed-in listener as listener for all events dispatched
by this event broadcaster. The function returns true
if the listener have been added at the end of the call. If the
listener is already registered in this event broadcaster the function
returns false.
Note : The addListener function doesn't accept functions
as listener, functions could only register for a single event.
Parameters
| listener:Object — the listener object to add as global listener
|
Returns
| Boolean — true if the listener have been added during this call
|
Throws
| — IllegalArgumentException — If the passed-in listener
listener doesn't match the listener type supported by this event
broadcaster
|
| |
| — IllegalArgumentException — If the passed-in listener
is a function
|
public function broadcastEvent(e:Event):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Broadcast the passed-in event object to listeners
according to the event's type. The event is broadcasted
to both listeners registered specifically for this event
type and global listeners in the broadcaster.
If the target property of the passed-in event
is null, it will be set using the value of the
source property of this event broadcaster.
Parameters
| e:Event — event object to broadcast
|
Throws
| — UnsupportedOperationException — If one listener is an object
which have neither a function with the same name than the event type nor
a function called handleEvent
|
public function dispatchEvent(o:Object):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Broadcast an event using an anonymous object.
The only requirement for objects passed as argument in
this function is that they must have a type
property. The target property will be set
with this event broadcaster's source if there is no source
specified.
The concret event object broadcasted to listener is a
DynBasicEvent decorated with the property
of the passed-in object.
Parameters
| o:Object — an anonymous object used to decorate a
DynBasicEvent
|
Throws
| — UnsupportedOperationException — If one listener is an object
which have neither a function with the same name than the event type nor
a function called handleEvent
|
public static function getInstance():EventBroadcaster
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns a static instance of the class.
The presence of a getInstance method in the
EventBroadcaster class doesn't mean that the class
is a singleton, it's simply a convenient way to let application
developers broadcasting events that correspond to user gestures
and requests over the whole application.
Returns
public function getListenerCollection(type:String = null):Collection
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns a Collection view of listeners for the passed-in
event type. The returned collection is a reference to the internal collection
of this event broadcaster, resulting that there's no guarantee that collection
cannot be altered by another object. If the type parameter is
omitted, the function returns the collection of global listeners objects (all
objects that haven't register for a specific event).
Parameters
| type:String (default = null) — the event name for which get a collection, if not
defined, the collection of global listeners is returned
|
Returns
| Collection —
Collection of listeners corresponding to the
passed-in event type
|
public function hasListenerCollection(type:String):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this event broadcaster has
listeners for the passed-in event type.
Parameters
| type:String — name of the event for which look for listener
|
Returns
| Boolean — true if this event broadcaster has
listeners for the passed-in event type
|
public function isEmpty():Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if this event broadcaster contains
no listeners for any event.
Returns
| Boolean — true if this event broadcaster contains
no listeners for any event.
|
public function isRegistered(listener:Object, type:String = null):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns true if the passed-in listener object is registered
as listener for the passed-in event type. If the type parameter
is omitted, the function returns true only if the listener is
registered as global listener.
Note : the listener could be either an object or a function.
Parameters
| listener:Object — object to look for registration
|
| |
| type:String (default = null) — event type to look at
|
Returns
| Boolean — true if the passed-in listener should receive notification
of the passed-in event type
|
public function removeAllListeners():void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes all listeners registered in this event broadcaster.
public function removeEventListener(type:String, listener:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes the passed-in listener for listening the specified event. The
listener could be either an object or a function.
Parameters
| type:String — name of the event for which unregister the listener
|
| |
| listener:Object — object or function to be unregistered
|
Returns
| Boolean — true if the listener have been successfully removed
as listener for the passed-in event
|
public function removeListener(listener:Object):Boolean
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes the passed-in listener object from this event
broadcaster. The object is removed as listener for all
events the broadcaster may dispatch.
Parameters
| listener:Object — the listener object to remove from
this event broadcaster object
|
Returns
| Boolean — true if the object have been successfully
removed from this event broadcaster
|
Throws
| — IllegalArgumentException — If the passed-in listener
is a function
|
public function removeListenerCollection(type:String):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Removes the complete collection of listeners for a specified
event type.
Parameters
| type:String — the event type for which remove all listeners
|
public function setListenerType(type:Class):void
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Defines the type of listeners this event broadcaster support.
Functions are not concerned by this restriction.
Parameters
| type:Class — the type of the listener this event broadcaster support
|
Throws
| — IllegalArgumentException — If one of the listener
already contained in this event broadcaster doesn't match the
passed-ni type
|
public function toString():String
| Player version: | Flash Player 9.0 |
| Language version: | ActionScript 3.0 |
Returns the String representation of
this object.
The function return a string like
com.bourre.events::EventBroadcaster<ListenerType>
for a typed broadcaster. The string between the <
and > is the name of the type of the broadcaster's
global listeners. If the broadcaster is an untyped broadcaster
the function will simply return the result of the
PixlibStringifier.stringify call.
Returns
| String — String representation of
this object.
|
LowRA API documentation 2008- 2009
Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1
mer. févr. 25 2009, 9:22 AM GMT+01:00