Skip to content

Instantly share code, notes, and snippets.

@emanuel-sanabria-developer
Created April 7, 2015 13:11
Show Gist options
  • Save emanuel-sanabria-developer/0803b67b3fb53ece2d81 to your computer and use it in GitHub Desktop.
Save emanuel-sanabria-developer/0803b67b3fb53ece2d81 to your computer and use it in GitHub Desktop.
function ObserverList() {
this.list = [];
};
ObserverList.prototype.add = function(obj) {
this.list.push(obj);
};
ObserverList.prototype.removeAt = function( index ){
this.list.splice( index, 1 );
};
function Subject() {
this.observers = new ObserverList();
};
Subject.prototype.addObserver = function (observer) {
this.observers.add(observer);
};
Subject.prototype.removeObserver = function( observer ){
this.observers.removeAt( this.observers.list.indexOf( observer, 0 ));
};
Subject.prototype.notify = function( context ){
var observerCount = this.observers.list.length;
for(var i=0; i < observerCount; i++){
this.observers.list[i].update( context );
}
};
function Observer(){
this.update = function(){
// ...
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment