Skip to content

Instantly share code, notes, and snippets.

@prisoner
Created July 28, 2011 18:08
Show Gist options
  • Save prisoner/1112133 to your computer and use it in GitHub Desktop.
Save prisoner/1112133 to your computer and use it in GitHub Desktop.
observer pattern for js
var c1312 = c1312 || {};
c1312.Observable = function() {
this._observers = [];
}
c1312.Observable.prototype = {
addObserver: function(o) {
this._observers.push(o);
},
deleteObserver: function(o) {
for(var i = 0; i < this._observers.length; i++ ) {
if(this._observers[i] == o)
this._observers.splice(i,1);
}
},
notifyObservers: function(msg) {
for(var i = 0; i < this._observers.length; i++ ) {
this._observers[i](msg);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment