Created
June 4, 2017 20:15
-
-
Save JBreit/56f54b49bda17a6e9a2d5c366c603631 to your computer and use it in GitHub Desktop.
Observable
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
const node = document.querySelector('input[name="test"]'); | |
const p = document.querySelector('p'); | |
function Observable(subscribe) { | |
this.subscribe = subscribe; | |
} | |
Observable.fromEvent = (element, name) => { | |
return new Observable((observer) => { | |
const callback = (event) => observer.next(event); | |
element.addEventListener(name, callback, false); | |
return () => element.removeEventListener(name, callback, false); | |
}); | |
}; | |
const input$ = Observable.fromEvent(node, 'input'); | |
const unsubscribe = input$.subscribe({ | |
next: (event) => { | |
p.innerHTML = event.target.value; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment