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
|
/**
*
*/
PIE.Observable = function() {
/**
* List of registered observer functions
*/
this.observers = [];
/**
* Hash of function ids to their position in the observers list, for fast lookup
*/
this.indexes = {};
};
PIE.Observable.prototype = {
observe: function( fn ) {
var id = PIE.Util.getUID( fn ),
indexes = this.indexes,
observers = this.observers;
if( !( id in indexes ) ) {
indexes[ id ] = observers.length;
observers.push( fn );
}
},
unobserve: function( fn ) {
var id = PIE.Util.getUID( fn ),
indexes = this.indexes;
if( id && id in indexes ) {
delete this.observers[ indexes[ id ] ];
delete indexes[ id ];
}
},
fire: function() {
var o = this.observers,
i = o.length;
while( i-- ) {
o[ i ] && o[ i ]();
}
}
};
|