Skip to content

Instantly share code, notes, and snippets.

@benlesh
Last active August 29, 2015 14:18
Show Gist options
  • Save benlesh/ac93b24f84fe31a53135 to your computer and use it in GitHub Desktop.
Save benlesh/ac93b24f84fe31a53135 to your computer and use it in GitHub Desktop.
var Observable = Rx.Observable;
// get a stream of keypresses (or even keyups)
var keypresses = Observable.fromEvent(myinput, 'keypress');
var subscription;
// throttled to every 500ms
subscription = keypresses.throttle(500).
// to a stream of values
map(function() {
return myinput.value;
}).
// to a stream of updates, cancelling out of "old" ones that haven't come back
flatMapLatest(function(value) {
// HACK: I'm not sure it's Rx.DOM.jsonp, but it's something like that.
return Rx.DOM.jsonpRequest('/get/my/values');
}).
// subscribe to the stream and update your DOM
subscribe(function(valuesFromServer) {
//TODO: update the dome with your array of valuesFromServer
console.log(valuesFromServer); // [thing1, thing2, etc];
});
// LATER: when you want to unbind this event, call dispose on the returned subscription/disposable:
subscription.dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment