Created
December 3, 2012 07:59
-
-
Save josher19/4193516 to your computer and use it in GitHub Desktop.
Plugin to allow use of RxJS in jQuery
This file contains hidden or 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
jQuery.fn.toObservable = jQuery.fn.toObservable || function (eventName, action) { | |
var dom = this; | |
return Rx.Observable.create(function(observer) { | |
var handler = function(ev) { | |
if (action !== undefined) { | |
action(ev); | |
} | |
else { | |
if (ev && ev.preventDefault) { | |
ev.preventDefault(); | |
} | |
} | |
observer.onNext(ev); | |
}; | |
dom.bind(eventName, handler); | |
return function() { | |
dom.unbind(eventName, handler); | |
}; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Example Usage:
test('listening to events', function() {
var received = '';
var subscription =
$(document)
.toObservable('foo')
.subscribe(function(e) { received += e.payload; });
});
// based on:
// https://github.com/mattpodwysocki/RxJSKoans/blob/master/koans/Answers/lesson4-Events.js