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