-
Notifications
You must be signed in to change notification settings - Fork 0
/
observable.js
57 lines (45 loc) · 1.44 KB
/
observable.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
47
48
49
50
51
52
53
54
55
56
57
/*
Class for an observable - has observers which it can notify about events.
*/
function observable() {
employee.call(this);
/*
An array of things observing this observable.
Let this observable know what's watching it so it can notify them of events.
*/
var observers = [];
this.__defineGetter__("observers", function(){ return observers });
}
observable.prototype = {
/*
Add the given observer to this observable's list of observers.
The observer will be notified of any events that happen on the observable.
*/
listen: function(observer) {
if(this.observers.indexOf(observer)==-1) this.observers.push(observer);
},
/*
Fire an event with the given type and source object.
Doing this will call the method "onType" for each observer if it exists.
Args will be passed along preceeded by the event source to this method.
The source can be useful if listening to multiple things which can fire the same event.
*/
event: function(type, source /*, args*/) {
//no point in doing any work if there aren't any observers
if(this.observers.length > 0) {
if(!defined(source)) {
source = this;
}
type = type[0].toUpperCase() + type.substring(1);
var args = Array.prototype.slice.call(arguments, 2);
args.unshift(source);
this.observers.forEach(function(obs) {
var handler = obs["on" + type];
if(typeof handler == "function") {
handler.apply(obs, args);
}
});
}
}
}
extend(observable, employee);