Created
October 12, 2017 23:29
-
-
Save jairoFernandez/e7c60de9d429017d62a3c05028e3e0e9 to your computer and use it in GitHub Desktop.
Prueba
This file contains hidden or 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
var Sinco = function(){}; | |
function ObserverList(){ | |
this.observerList = []; | |
} | |
ObserverList.prototype.add = function(obj){ | |
return this.observerList.push(obj); | |
}; | |
ObserverList.prototype.get = function(index){ | |
if(index > -1 && index < this.observerList.length){ | |
return this.observerList[index]; | |
} | |
}; | |
ObserverList.prototype.count = function(){ | |
return this.observerList.length; | |
} | |
ObserverList.prototype.removeAt = function(index){ | |
this.observerList.splice(index, 1); | |
} | |
ObserverList.prototype.indexOf = function(obj, startIndex){ | |
var i = startIndex; | |
while(i < this.observerList.length){ | |
if(this.observerList[i]===obj){ | |
return i; | |
} | |
i++; | |
} | |
return -1; | |
} | |
var SincoObservable = function(obj, event){ | |
console.log("Creado observable"); | |
this.observers = new ObserverList(); | |
} | |
SincoObservable.prototype.subscribe = function(observer){ | |
console.log("Bienvenido a la subscripción: " + observer); | |
this.observers.add(observer); | |
} | |
SincoObservable.prototype.notify = function(context){ | |
var observerCount = this.observers.count(); | |
for(var i=0; i < observerCount; i++){ | |
this.observers.get(i)(context); | |
} | |
} | |
SincoObservable.prototype.unSubscribe = function(observer){ } | |
function demo(data){ | |
this.name = data.name; | |
this.update = function(){ | |
console.log("Actualizo -- " + this.name); | |
} | |
} | |
var prueba = new demo({name:"Jairo Fernández"}); | |
var observer1 = "Observer 1"; | |
var observable = new SincoObservable(prueba, prueba.update); // Observar el objeto prueba y espero cambios de su funcion update | |
// El objeto observer1 va a suscribirse a el objeto observable | |
observable.subscribe(observer1); | |
prueba.update(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment