Created
April 7, 2015 13:11
-
-
Save emanuel-sanabria-developer/0803b67b3fb53ece2d81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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