Skip to content

Instantly share code, notes, and snippets.

@jreisstudio
Created December 11, 2012 18:22
Show Gist options
  • Save jreisstudio/4260821 to your computer and use it in GitHub Desktop.
Save jreisstudio/4260821 to your computer and use it in GitHub Desktop.
Simple Observer
function SimpleObserver(){
var subscribersList=[];
this.subscribe = function (_objectSub, _methodSub,_id){
var subscriber = {}
subscriber.subObject=_objectSub;
subscriber.subMethod=_methodSub;
subscriber.subId = _id;
subscribersList.push(subscriber);
}
this.unsubscribe = function (_id){
for(i=0;i<subscribersList.length;i++){
if(subscribersList[i].subId==_id){
subscribersList.splice(i,1);
}
}
}
this.publish = function (_publishNotification){
for(i=0;i<subscribersList.length;i++){
try{
subscribersList[i].subObject[subscribersList[i].subMethod](_publishNotification);
}catch(error){
console.log(error);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment