-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventHandler.js
53 lines (49 loc) · 1.24 KB
/
EventHandler.js
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
/*
* EventHandler is a great class to handle custom events and is way faster than jQuery
*
* EventHandler.bind (eventName:String, callback:Function ) Subscribe on a certain event
* EventHandler.trigger(eventName:String, arguments... ) Trigger the event with a certain amount of arguments
* EventHandler.unbind (eventName:String, callback:Function ) Unsubscribe to a certain event
*
* @author Edwin Veldhuizen <[email protected]>
*/
var EventHandler = (function (){
var cache = {},
trigger = function (topic) {
if (cache[topic]) {
var thisTopic = cache[topic],
i = thisTopic.length,
funct = null,
args = Array.prototype.slice.call(arguments);
args.shift();
while(i--){
funct = thisTopic[i];
funct.apply(topic,args);
}
}
},
bind = function (topic, callback) {
if (!cache[topic]) cache[topic] = [];
cache[topic].push(callback);
return [topic, callback];
},
unbind = function (handle, destroy) {
var t = handle[0];
ct = cache[t];
if (ct) {
if(destroy){
delete cache[t];
}else{
var len = ct.length;
while(len--){
ct.splice(ct[len], 1);
}
}
}
};
return {
trigger: trigger,
bind: bind,
unbind: unbind
};
}());