Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 2a82f19

Browse files
Updating documentation and bindings
1 parent 5a693f6 commit 2a82f19

File tree

2 files changed

+251
-2
lines changed

2 files changed

+251
-2
lines changed

lib/rx.jquery.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,48 @@
66
(function(global, $) {
77
var root = global.Rx,
88
observable = root.Observable,
9+
observableProto = observable.prototype,
910
asyncSubject = root.AsyncSubject,
1011
observableCreate = observable.create,
1112
observableCreateWithDisposable = observable.createWithDisposable,
1213
disposableEmpty = root.Disposable.empty,
1314
slice = Array.prototype.slice,
1415
proto = $.fn;
1516

17+
$.Deferred.prototype.toObservable = function () {
18+
var subject = new asyncSubject();
19+
parent.done(function () {
20+
subject.onNext(slice.call(arguments));
21+
subject.onCompleted();
22+
}).fail(function () {
23+
subject.onError(slice.call(arguments));
24+
});
25+
return subject;
26+
};
27+
28+
observableProto.toDeferred = function () {
29+
var deferred = $.Deferred();
30+
this.subscribe(function (value) {
31+
deferred.resolve(value);
32+
}, function (e) {
33+
deferred.reject(e);
34+
});
35+
return deferred;
36+
};
37+
38+
$.Callbacks.prototype.toObservable = function () {
39+
var parent = this;
40+
return observableCreate(function (observer) {
41+
var handler = function(values) {
42+
observer.onNext(values);
43+
};
44+
parent.add(handler);
45+
return function () {
46+
parent.remove(handler);
47+
};
48+
});
49+
};
50+
1651
proto.onAsObservable = function (events, selector, data) {
1752
var parent = this;
1853
return observableCreate(function(observer) {
@@ -62,6 +97,15 @@
6297
};
6398
});
6499
};
100+
proto.changeAsObservable = function (eventData) {
101+
return this.bindAsObservable('change', eventData);
102+
};
103+
proto.clickAsObservable = function (eventData) {
104+
return this.bindAsObservable('click', eventData);
105+
};
106+
proto.dblclickAsObservable = function (eventData) {
107+
return this.bindAsObservable('dblclick', eventData);
108+
};
65109
proto.focusAsObservable = function(eventData) {
66110
return this.bindAsObservable('focus', eventData);
67111
};
@@ -183,6 +227,14 @@
183227
});
184228
return subject;
185229
};
230+
proto.fadeToggleAsObservable = function(duration, easing) {
231+
var subject = new asyncSubject();
232+
this.fadeToggle(duration, easing, function() {
233+
subject.onNext(this);
234+
subject.onCompleted();
235+
});
236+
return subject;
237+
};
186238
proto.slideDownAsObservable = function(duration) {
187239
var subject = new asyncSubject();
188240
this.slideDown(duration, function() {
@@ -207,7 +259,15 @@
207259
});
208260
return subject;
209261
};
210-
var ajaxAsObservable = _jQuery.ajaxAsObservable = function(settings) {
262+
proto.toggleAsObservable = function(duration, easing) {
263+
var subject = new asyncSubject();
264+
this.toggle(duration, easing, function() {
265+
subject.onNext(this);
266+
subject.onCompleted();
267+
});
268+
return subject;
269+
};
270+
var ajaxAsObservable = $.ajaxAsObservable = function(settings) {
211271
var subject = new asyncSubject(), internalSettings = {};
212272
for (var k in settings) {
213273
internalSettings[k] = settings[k];

readme.md

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,190 @@
1-
This project provides Reactive Extensions for JavaScript (RxJS) bindings for jQuery to abstract over the event binding, Ajax and Deferreds. The Reactive Extensions are not included with this release and must be installed separately.
1+
jQuery Bindings for the Reactive Extensions for JavaScript
2+
==========================================================
3+
## OVERVIEW
4+
5+
This project provides Reactive Extensions for JavaScript (RxJS) bindings for jQuery to abstract over the event binding, Ajax and Deferreds. The RxJS libraries are not included with this release and must be installed separately.
6+
7+
## GETTING STARTED
8+
9+
There are a number of ways to get started with L2O.js depending on your environment.
10+
11+
### Download the Source
12+
13+
To download the source of the jQuery Bindings for the Reactive Extensions for JavaScript, type in the following:
14+
15+
git clone https://github.com/Reactive-Extensions/rxjs-jquery.git
16+
cd ./rxjs-jquery
17+
18+
### Installing with NuGet
19+
20+
Coming soon!
21+
22+
### Getting Started with the jQuery Bindings
23+
24+
Let's walk through a simple yet powerful example of the Reactive Extensions for JavaScript Bindings for jQuery, autocomplete. In this example, we will take user input from a textbox and trim and throttle the input so that we're not overloading the server with requests for suggestions.
25+
26+
We'll start out with a basic skeleton for our application with script references to jQuery, RxJS, RxJS Time-based methods, and the RxJS Bindings for jQuery, along with a textbox for input and a list for our results.
27+
28+
<script type="text/javascript" src="jquery.js"></script>
29+
<script type="text/javascript" src="rx.min.js"></script>
30+
<script type="text/javascript" src="rx.time.min.js"></script>
31+
<script type="text/javascript" src="rx-jquery.js"><script>
32+
<script type="text/javascript">
33+
$(function () {
34+
...
35+
})();
36+
</script>
37+
...
38+
<input id="textInput" type="text"></input>
39+
<ul id="results"></ul>
40+
...
41+
42+
The goal here is to take the input from our textbox and throttle it in a way that it doesn't overload the service with requests. To do that, we'll get the reference to the textInput using jQuery, then bind to the 'keyup' event using the keyupAsObservable method which then takes the jQuery object and transforms it into an RxJS Observable.
43+
44+
var throttledInput = $('#textInput')
45+
.keyupAsObservable()
46+
47+
Since we're only interested in the text, we'll use the [select](http://msdn.microsoft.com/en-us/library/hh244311(v=VS.103).aspx) method to take the event object and return the target's value.
48+
49+
.select( function (ev) {
50+
return $(ev.target).val();
51+
})
52+
53+
We're also not interested in query terms less than two letters, so we'll trim that user input by using the [where](http://msdn.microsoft.com/en-us/library/hh229267(v=VS.103).aspx) method returning whether the string length is appropriate.
54+
55+
.where( function (text) {
56+
return text.length > 2;
57+
})
58+
59+
We also want to slow down the user input a little bit so that the external service won't be flooded with requests. To do that, we'll use the [throttle](http://msdn.microsoft.com/en-us/library/hh229298(v=VS.103).aspx) method with a timeout of 500 milliseconds, which will ignore your fast typing and only return a value after you have paused for that time span.
60+
61+
.throttle(500)
62+
63+
Lastly, we only want distinct values in our input stream, so we can ignore requests that are not unique, for example if I copy and paste the same value twice, the request will be ignored.
64+
65+
.distinctUntilChanged();
66+
67+
Putting it all together, our throttledInput looks like the following:
68+
69+
var throttledInput = $('#textInput')
70+
.keyupAsObservable()
71+
.select( function (ev) {
72+
return $(ev.target).val();
73+
})
74+
.where( function (text) {
75+
return text.length > 2;
76+
})
77+
.throttle(500)
78+
.distinctUntilChanged();
79+
80+
Now that we have the throttled input from the textbox, we need to query our service, in this case, the Wikipedia API, for suggestions based upon our input. To do this, we'll create a function called searchWikipedia which calls the jQuery.ajaxAsObservable method which wraps the existing jQuery Ajax request in an RxJS [AsyncSubject](http://msdn.microsoft.com/en-us/library/hh229363(v=VS.103).aspx).
81+
82+
function searchWikipedia(term) {
83+
return $.ajaxAsObservable({
84+
url: 'http://en.wikipedia.org/w/api.php',
85+
data: { action: 'opensearch',
86+
search: term,
87+
format: 'json' }
88+
dataType: 'jsonp'
89+
});
90+
}
91+
92+
Now that the Wikipedia Search has been wrapped, we can tie together throttled input and our service call. In this case, we will call select on the throttledInput to then take the text from our textInput and then use it to query Wikipedia, filtering out empty records. Finally, to deal with concurrency issues, we'll need to ensure we're getting only the latest value. Issues can arise with asynchronous programming where an earlier value, if not cancelled properly, can be returned before the latest value is returned, thus causing bugs. To ensure that this doesn't happen, we have the [switchLatest](http://msdn.microsoft.com/en-us/library/hh229197(v=VS.103).aspx) method which returns only the latest value.
93+
94+
var suggestions = throttledInput.select( function (text) {
95+
return searchWikipedia(text);
96+
})
97+
.where( function (data) {
98+
return data.length == 2 && data[1].length > 0;
99+
})
100+
.switchLatest();
101+
102+
Finally, we'll subscribe to our observable by calling subscribe which will receive the results and put them into an unordered list. We'll also handle errors, for example if the server is unavailable by passing in a second function which handles the errors.
103+
104+
suggestions.subscribe( function (data) {
105+
var selector = $('#results');
106+
selector.clear();
107+
$.each(data[1], function (_, text) {
108+
$('<li>' + text + '</li>').appendTo(selector);
109+
});
110+
}, function (e) {
111+
selector.clear();
112+
$('<li>Error: ' + e + '</li>').appendTo('#results');
113+
});
114+
115+
We've only scratched the surface of this library in this simple example.
116+
117+
### Implemented Bindings
118+
119+
* jQuery Events
120+
* [bind](http://api.jquery.com/bind/) - bindAsObservable
121+
* [delegate](http://api.jquery.com/delegate/) - delegateAsObservable
122+
* [live](http://api.jquery.com/live/) - liveAsObservable
123+
* [on](http://api.jquery.com/on/) - onAsObservable
124+
* [one](http://api.jquery.com/one/) - oneAsObservable
125+
126+
* jQuery Event Shortcuts
127+
* [change](http://api.jquery.com/change/) - changeAsObservable
128+
* [click](http://api.jquery.com/click/) - clickAsObservable
129+
* [dblclick](http://api.jquery.com/dblclick/) - dblclickAsObservable
130+
* [focus](http://api.jquery.com/focus/) - focusAsObservable
131+
* [focusin](http://api.jquery.com/focusin/) - focusinAsObservable
132+
* [focusout](http://api.jquery.com/focusout/) - focusoutAsObservable
133+
* [hover](http://api.jquery.com/hover/) - hoverAsObservable
134+
* [keydown](http://api.jquery.com/keydown/) - keydownAsObservable
135+
* [keypress](http://api.jquery.com/keypress/) - keypressAsObservable
136+
* [keyup](http://api.jquery.com/keyup/) - keyupAsObservable
137+
* [load](http://api.jquery.com/load/) - loadAsObservable
138+
* [mousedown](http://api.jquery.com/mousedown/) - mousedownAsObservable
139+
* [mouseenter](http://api.jquery.com/mouseenter/) - mouseenterAsObservable
140+
* [mouseleave](http://api.jquery.com/mouseleave/) - mouseleaveAsObservable
141+
* [mousemove](http://api.jquery.com/mousemove/) - mousemoveAsObservable
142+
* [mouseover](http://api.jquery.com/mouseover/) - mouseoverAsObservable
143+
* [mouseup](http://api.jquery.com/mouseup/) - mouseupAsObservable
144+
* [ready](http://api.jquery.com/ready/) - readyAsObservable
145+
* [resize](http://api.jquery.com/resize/) - resizeAsObservable
146+
* [scroll](http://api.jquery.com/scroll/) - scrollAsObservable
147+
* [select](http://api.jquery.com/select/) - selectAsObservable
148+
* [submit](http://api.jquery.com/submit/) - submitAsObservable
149+
* [unload](http://api.jquery.com/unload/) - unloadAsObservable
150+
151+
* jQuery Effects
152+
* [animate](http://api.jquery.com/animate/) - animateAsObservable
153+
* [fadeIn](http://api.jquery.com/fadeIn/) - fadeInAsObservable
154+
* [fadeOut](http://api.jquery.com/fadeOut/) - fadeOutAsObservable
155+
* [fadeTo](http://api.jquery.com/fadeTo/) - fadeToAsObservable
156+
* [fadeToggle](http://api.jquery.com/fadeToggle/) - fadeToggleAsObservable
157+
* [hide](http://api.jquery.com/hide/) - hideAsObservable
158+
* [show](http://api.jquery.com/show/) - showAsObservable
159+
* [slideDown](http://api.jquery.com/slideDown/) - slideDownAsObservable
160+
* [slideToggle](http://api.jquery.com/slideToggle/) - slideToggleAsObservable
161+
* [slideUp](http://api.jquery.com/slideUp/) - slideUpAsObservable
162+
163+
* Ajax Methods
164+
* [ajax](http://api.jquery.com/jQuery.ajax/) - ajaxAsObservable
165+
* [get](http://api.jquery.com/jQuery.get/) - getAsObservable
166+
* [getJSON](http://api.jquery.com/jQuery.getJSON/) - getJSONAsObservable
167+
* [getScript](http://api.jquery.com/jQuery.getScript/) - getScriptAsObservable
168+
* [post](http://api.jquery.com/jQuery.post/) - postAsObservable
169+
170+
* Deferreds
171+
* Deferred.toObservable
172+
* Rx.Observable.toDeferred
173+
174+
* Callbacks
175+
* Callbacks.toObservable
176+
177+
## LICENSE
178+
179+
Copyright 2011 Microsoft Corporation
180+
181+
Licensed under the Apache License, Version 2.0 (the "License");
182+
you may not use this file except in compliance with the License.
183+
You may obtain a copy of the License at
184+
http://www.apache.org/licenses/LICENSE-2.0
185+
186+
Unless required by applicable law or agreed to in writing, software
187+
distributed under the License is distributed on an "AS IS" BASIS,
188+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189+
See the License for the specific language governing permissions and
190+
limitations under the License.

0 commit comments

Comments
 (0)