-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.js
More file actions
40 lines (32 loc) · 870 Bytes
/
observer.js
File metadata and controls
40 lines (32 loc) · 870 Bytes
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
def('observer', function(){
function Observer(){
this.observerList = [];
}
Observer.prototype.addObserver = function(obj){
this.observerList.push(obj);
};
Observer.prototype.removeObserver = function(obj){
for (var i = 0; i < this.observerList.length; i++) {
if( this.observerList[i] === obj ){
this.observerList.slice(i,1);
}
}
};
Observer.prototype.removeAllObserver = function(){
this.observerList = [];
};
Observer.prototype.getAllObservers = function(){
return this.observerList;
};
Observer.prototype.getObserverByIndex = function(index){
if( index > -1 && index < this.observerList.length ){
return this.observerList[index];
}
};
Observer.prototype.notify = function(context){
for (var i = 0; i < this.observerList.length; i++) {
this.observerList[i].update(context);
}
};
module.exports = observer;
});