013039787e76b1afd55a6ab4557aad72ed4baae1
[ccsdk/features.git] /
1 (function( app ) {
2
3         var ux = app.ns("ux");
4
5         ux.Observable = ux.Class.extend((function() {
6                 return {
7                         init: function() {
8                                 this.observers = {};
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 ] );
12                                         }
13                                 }
14                         },
15                         _getObs: function( type ) {
16                                 return ( this.observers[ type.toLowerCase() ] || ( this.observers[ type.toLowerCase() ] = [] ) );
17                         },
18                         on: function( type, fn, params, thisp ) {
19                                 this._getObs( type ).push( { "cb" : fn, "args" : params || [] , "cx" : thisp || this } );
20                                 return this;
21                         },
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 ) );
26                                 } );
27                                 return this;
28                         },
29                         removeAllObservers: function() {
30                                 this.observers = {};
31                         },
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 );
35                                 if(index !== -1) {
36                                         obs.splice( index, 1 );
37                                 }
38                                 return this; // make observable functions chainable
39                         },
40                         hasObserver: function( type ) {
41                                 return !! this._getObs( type ).length;
42                         }
43                 };
44         })());
45
46 })( this.app );