Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- });
Add Comment
Please, Sign In to add comment