-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventEmitter.js
41 lines (32 loc) · 900 Bytes
/
eventEmitter.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
/**
* Define Server class
*/
//define(['events'],function(events) {
define(function() {
// attributes
var EventEmitter = function() {
this.events = {};
};
// methods
EventEmitter.prototype = {
fire : function(event, obj) {
var actions = this.events[event],
scope = obj.scope || {},
datas = obj.data || {};
if (actions){
for (var i = 0; i < actions.length; i++) {
actions[i].call(scope, datas);
if(obj.fn && typeof obj.fn === "function")
obj.fn.call(scope);
}
}
},
on : function(event, action) {
if (!this.events[event]){
this.events[event] = [];
}
this.events[event].push(action);
}
};
return new EventEmitter();
});