Created
September 15, 2015 14:07
-
-
Save seanislegend/1740c4e10a324b6051ba to your computer and use it in GitHub Desktop.
RxJS - Keyboard input
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
/* | |
* The element to use, in this case a search field. | |
*/ | |
let input = document.getElementById('input'); | |
let results = document.getElementById('results'); | |
/** | |
* Create an Observable that will receive all keyup events | |
*/ | |
let source = Rx.Observable.fromEvent(input, 'keyup') | |
/** | |
* Limit amount of 'notifications' to receive | |
*/ | |
.throttle(150) | |
/** | |
* Map through event object and pull out the field's value | |
*/ | |
.map((e) => e.target.value) | |
/** | |
* Flatten all search requests into a single sequence | |
*/ | |
.flatMap(getAsyncSearch) | |
/** | |
* Pass the data to another function | |
*/ | |
.subscribe(showResults); | |
function showResults (data) { | |
let formattedData = []; | |
/** | |
* Use lodash's forEach function to loop through our results object | |
*/ | |
forEach(data, (item) => { | |
formattedData.push('<h1>' + item.title + '</h1><p>' + item.body + '</p>'); | |
}); | |
/** | |
* Add the formatted data to the results element | |
results.innerHTML = formattedData.join('<hr>'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment