5 ux.Observable = ux.Class.extend((function() {
9 for( var opt in this.config ) { // automatically install observers that are defined in the configuration
10 if( opt.indexOf( 'on' ) === 0 ) {
11 this.on( opt.substring(2) , this.config[ opt ] );
15 _getObs: function( type ) {
16 return ( this.observers[ type.toLowerCase() ] || ( this.observers[ type.toLowerCase() ] = [] ) );
18 on: function( type, fn, params, thisp ) {
19 this._getObs( type ).push( { "cb" : fn, "args" : params || [] , "cx" : thisp || this } );
22 fire: function( type ) {
23 var params = Array.prototype.slice.call( arguments, 1 );
24 this._getObs( type ).slice().forEach( function( ob ) {
25 ob["cb"].apply( ob["cx"], ob["args"].concat( params ) );
29 removeAllObservers: function() {
32 removeObserver: function( type, fn ) {
33 var obs = this._getObs( type ),
34 index = obs.reduce( function(p, t, i) { return (t.cb === fn) ? i : p; }, -1 );
36 obs.splice( index, 1 );
38 return this; // make observable functions chainable
40 hasObserver: function( type ) {
41 return !! this._getObs( type ).length;