My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
*
* @class GlobalDispatcher
* @description Simple way to dispatch global event notifications.
* Useful for passing around events between objects when you have no clue
* what the Object tree looks like or what Object is broadcasting.
* @author pixelrevision
* @version 1.0
*
*/

class com.pixelrevision.events.GlobalDispatcher{

private static var _events:Array = new Array();

/**
* Adds a listener to the broadcast of a certian event.
* @param eventName The name of the event to listen for.
* @param scope The object that contains the function.
* @param func The function to call.
*/
public static function addEventListener(eventName:String, scope:Object, func:Function):Void{
var eventListener:Object = new Object();
eventListener.func = func;
eventListener.scope = scope;
if(_events[eventName] == undefined){
_events[eventName] = new Array();
}
_events[eventName].push(eventListener);
}

/**
* Removes a listener to the broadcast of a specific event.
* @param eventName The name of the event listener to remove.
* @param scope The object that is associated with the listener.
* @param func The function that is associated with the listener.
*/
public static function removeEventListener(eventName:String, scope:Object, func:Function):Void{
// remove the event
for(var data in _events[eventName]){
if(scope == undefined){
_events[eventName].splice(data, 1);
}else if(func == undefined){
// delete all events for a particular scope
if(_events[eventName][data].scope == scope){
_events[eventName].splice(data, 1);
}
}else{
// delete all events for a particular scope
if(_events[eventName][data].scope == scope && _events[eventName][data].func == func){
_events[eventName].splice(data, 1);
}
}
}

// get rid of the nested array if it is empty
for(var i in _events){
if(_events[i].length == 0){
_events.splice(i, 1);
}
}
}

/**
* Broadcasts an event to all available listeners.
* @param eventName The name of the event to send.
* @param params A list of params to send to all listener functions.
*/
public static function dispatchEvent(eventName:String, params:Array):Void{
if(params == undefined){
params = [];
}
// apply the functions
for(var data in _events[eventName]){
if(_events[eventName][data].scope == undefined){
// remove the event listener, the scope is not available anymore
_events[eventName].splice(data, 1);
}
_events[eventName][data].func.apply(_events[eventName][data].scope, params);
}
}

/**
* Checks if an event listener exists.
* @return Returns true if a listener exists.
*/
public static function hasEventListener(eventName:String):Boolean{
if(_events[eventName].length > 0){
return true;
}
return false;
}

/**
* A list of events currently in the cue
* @return An array with all events.
*/
public function get events():Array{
var events:Array = new Array();
for(var data in _events){
events.push(data);
}
return events;
}

}

Change log

r26 by pixelrevision on May 23, 2008   Diff
Added the event dispatcher
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3067 bytes, 106 lines
Powered by Google Project Hosting