Skip to content

Instantly share code, notes, and snippets.

@seanislegend
Created September 15, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanislegend/1740c4e10a324b6051ba to your computer and use it in GitHub Desktop.
Save seanislegend/1740c4e10a324b6051ba to your computer and use it in GitHub Desktop.
RxJS - Keyboard input
/*
* 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