Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created June 20, 2012 01:16
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 mattpodwysocki/2957550 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/2957550 to your computer and use it in GitHub Desktop.
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