Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Forked from heikela/valueAsObservable.js
Last active December 16, 2015 23:09
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 pyrtsa/5512257 to your computer and use it in GitHub Desktop.
Save pyrtsa/5512257 to your computer and use it in GitHub Desktop.
// jQuery extensions
$.fn.valueAsObservable = function valueAsObservable() {
var changes = $(this).changeAsObservable();
var keyUps = $(this).keyupAsObservable();
var values = changes.merge(keyUps).map(function (e) {
return e.target.value;
});
return values.startWith($(this).val()).distinctUntilChanged();
}
// Define some streams
var nameS = $('#user-info input[name=name]').valueAsObservable().subscribe(log);
// Throttling (if rx.time.js is included)
var throttledNameS = nameS.throttle(1000); // milliseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment