-
Notifications
You must be signed in to change notification settings - Fork 1
/
observer.js
46 lines (41 loc) · 1.43 KB
/
observer.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
function theSubject(){
this.handlerList = [];
}
theSubject.prototype = {
addObserver: function(obs) {
//add all the observers in an array.
this.handlerList.push(obs);
console.log('added observer', this.handlerList);
},
removeObserver: function(obs) {
//remove given observer from the array.
for ( var ii=0, length = this.handlerList.length; ii<length; ii++ ) {
if(this.handlerList[ii] === obs) {
this.handlerList.splice(ii--,1);
length--;
console.log('removed observer', this.handlerList);
}
}
},
notify: function(obs, context) {
//for all functions in handler, notify
var bindingContext = context || window;
this.handlerList.forEach(function(fn){
fn.call(bindingContext, obs);
});
}
};
function init() {
var theEventHandler = function(item) {
console.log("fired: " + item);
};
var subject = new theSubject();
subject.addObserver(theEventHandler); //adds the given function in handler list
subject.notify('event #1'); //calls the function once.
subject.addObserver(theEventHandler); // adds the given function one more time
subject.removeObserver(theEventHandler); //removes this function twice from the function list
subject.notify('event #2'); //notify doesn't call anything
subject.addObserver(theEventHandler); //adds the function again
subject.notify('event #3'); //calls it once with event 3
}
init();