-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclass.observable.js
36 lines (35 loc) · 921 Bytes
/
class.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
/**
* mixin Class.Observable
*
* var Person = Class.create(Class.Observable, {
* initialize: function(name){ this.name = name }
* })
*
* var jdd = new Person('John');
*
* jdd.observe(':say', console.log);
* jdd.fire(':say');
*
**/
// with corrections by John David Dalton and juanbond
(function(){
function getElement(object){
return (object._eventElement = object._eventElement ||
(document.body || document.documentElement)
.appendChild(new Element('div')));
}
Class.Observable = {
observe: function(eventName, handler) {
getElement(this).observe(eventName, handler);
return this;
},
stopObserving: function(eventName, handler) {
getElement(this).stopObserving(eventName, handler);
return this;
},
fire: function(eventName, memo) {
getElement(this).fire(eventName, memo);
return this;
}
}
})();