-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.js
32 lines (27 loc) · 831 Bytes
/
event.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
var eventManager = function () {
var that, subscribers;
that = {};
subscribers = {};
that.notify = function (event, data) {
if (subscribers[event] != undefined) {
for (var i = 0; i < subscribers[event].length; ++i) {
subscribers[event][i](data);
}
}
}
that.subscribe = function (event, handler) {
if (subscribers[event] == undefined) {
subscribers[event] = [];
}
subscribers[event].push(handler);
}
that.unsubscribe = function (event, handler) {
if (subscribers[event] != undefined) {
var idx = subscribers[event].indexOf(handler);
if (idx != -1) {
subscribers[event].splice(idx, 1);
}
}
}
return that;
};