Sha256: 9768335f06a58a9e8ce24027067b1f7a8fd629b3457e2f712e27981523762ba3

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

JS.Observable = new JS.Module('Observable', {
  extend: {
    DEFAULT_METHOD: 'update'
  },
  
  addObserver: function(observer, context) {
    (this.__observers__ = this.__observers__ || []).push({_block: observer, _context: context || null});
  },
  
  removeObserver: function(observer, context) {
    this.__observers__ = this.__observers__ || [];
    context = context || null;
    var i = this.countObservers();
    while (i--) {
      if (this.__observers__[i]._block === observer && this.__observers__[i]._context === context) {
        this.__observers__.splice(i,1);
        return;
      }
    }
  },
  
  removeObservers: function() {
    this.__observers__ = [];
  },
  
  countObservers: function() {
    return (this.__observers__ = this.__observers__ || []).length;
  },
  
  notifyObservers: function() {
    if (!this.isChanged()) return;
    var i = this.countObservers(), observer, block, context;
    while (i--) {
      observer = this.__observers__[i];
      block    = observer._block;
      context  = observer._context;
      if (JS.isFn(block)) block.apply(context || null, arguments);
      else block[context || JS.Observable.DEFAULT_METHOD].apply(block, arguments);
    }
  },
  
  setChanged: function(state) {
    this.__changed__ = !(state === false);
  },
  
  isChanged: function() {
    if (this.__changed__ === undefined) this.__changed__ = true;
    return !!this.__changed__;
  }
});

JS.Observable.include({
  subscribe:    JS.Observable.instanceMethod('addObserver'),
  unsubscribe:  JS.Observable.instanceMethod('removeObserver')
}, true);

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
spontaneous-0.1.0.alpha1 application/js/vendor/JS.Class-2.1.5/src/observable.js
acts_as_dashboard-0.4.1 generators/dashboard/templates/js.class-2.1.4/src/observable.js
acts_as_dashboard-0.4.0 generators/dashboard/templates/js.class-2.1.4/src/observable.js
acts_as_dashboard-0.3.3 generators/dashboard/templates/js.class-2.1.4/src/observable.js
acts_as_dashboard-0.3.2 generators/dashboard/templates/js.class-2.1.4/src/observable.js
acts_as_dashboard-0.3.0 generators/dashboard/templates/js.class-2.1.4/src/observable.js
acts_as_dashboard-0.1.0 generators/dashboard/templates/js.class-2.1.4/src/observable.js