Create a gist now

Instantly share code, notes, and snippets.

var element = document.querySelector('#textinput');
var throttledInput = Rx.Observable.fromEvent(element, 'keyup').select( function (ev) {
// Return text value for each key up
return ev.target.value;
}).throttle(
// Throttle input by 500ms
500
).where( function (text) {
// Only get input more than 2 characters
return text.length > 2;
}.distinctUntilChanged(
// Returns only distinct text values
);
// Combine together with an Ajax call
var serviceCall = throttledInput.select( function (text) {
// Call service wrapped as an Observable sequence
return queryService(text);
}.switchLatest(
// Trim out of order requests to ensure we only have the latest
);
// Subscribe to the service call
serviceCall.subscribe( function (results) {
// Handle the results
}, function (err) {
// Handle the errors
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment