forked from Kolyaj/iclass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (61 loc) · 1.81 KB
/
index.js
File metadata and controls
70 lines (61 loc) · 1.81 KB
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
exports.create = function(parent, proto) {
if (arguments.length < 2) {
proto = parent;
parent = Object;
}
proto = proto || {};
if (!proto.hasOwnProperty('constructor')) {
proto.constructor = function() {
parent.apply(this, arguments);
};
}
require('util').inherits(proto.constructor, parent);
mixin(proto.constructor.prototype, proto);
proto.constructor.superclass = parent.prototype;
return proto.constructor;
};
exports.Component = exports.create(require('events').EventEmitter, {
constructor: function(cfg) {
exports.Component.superclass.constructor.apply(this, arguments);
mixin(this, cfg);
this._listeners = [];
this._initComponent();
},
_initComponent: function() {
},
destroy: function() {
this._unAll();
},
_on: function(object, event, method) {
var ctx = this;
var listener = function() {
method.apply(ctx, arguments);
};
this._listeners.push([object, event, method, listener]);
object.on(event, listener);
},
_un: function(object, event, method) {
for (var i = 0; i < this._listeners.length; i++) {
var item = this._listeners[i];
if (item[0] == object && item[1] == event && item[2] == method) {
object.removeListener(event, item[3]);
this._listeners.splice(i, 1);
break;
}
}
},
_unAll: function() {
this._listeners.forEach(function(item) {
item[0].removeListener(item[1], item[3]);
});
this._listeners.length = 0;
}
});
function mixin(dst, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) {
dst[key]= src[key];
}
}
return dst;
}