• This repository has been archived on 21/Apr/2018
  • Stars
    star
    213
  • Rank 177,436 (Top 4 %)
  • Language
    JavaScript
  • Created over 12 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Reactive Extensions bindings for jQuery

Build Status Built with Grunt NPM version

RxJS-jQuery 1.1 - jQuery Bindings for the Reactive Extensions for JavaScript

OVERVIEW

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.

GETTING STARTED

There are a number of ways to get started with the jQuery Bindings for RxJS. The files are available on cdnjs and jsDelivr.

Download the Source

To download the source of the jQuery Bindings for the Reactive Extensions for JavaScript, type in the following:

git clone https://github.com/Reactive-Extensions/rxjs-jquery.git
cd ./rxjs-jquery

Installing with NPM

npm install rx-jquery

Installing with Bower

bower install rxjs-jquery

Installing with Jam

jam install rx-jquery

Installing with NuGet

PM> Install-Package RxJS-Bridges-jQuery

Getting Started with the jQuery Bindings

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.

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.

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="rx.lite.js"></script>
<script type="text/javascript" src="rx-jquery.js"><script>
<script type="text/javascript">
	$(function () {
		...
	})();
</script>
...
<input id="textInput" type="text"></input>
<ul id="results"></ul>
...

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.

var throttledInput = $('#textInput')
   .keyupAsObservable()

Since we're only interested in the text, we'll use the map method to take the event object and return the target's value.

	.map( function (ev) {
		return $(ev.target).val();
	})

We're also not interested in query terms less than two letters, so we'll trim that user input by using the filter method returning whether the string length is appropriate.

	.filter( function (text) {
		return text.length > 2;
	})

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 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.

	.throttle(500 /* ms */)

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.

	.distinctUntilChanged();

Putting it all together, our throttledInput looks like the following:

var throttledInput = $('#textInput')
	.keyupAsObservable()
	.map( function (ev) {
		return $(ev.target).val();
	})
	.filter( function (text) {
		return text.length > 2;
	})
	.throttle(500)
	.distinctUntilChanged();

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 Rx.AsyncSubject.

function searchWikipedia(term) {
	return $.ajaxAsObservable({
		url: 'http://en.wikipedia.org/w/api.php',
		data: { action: 'opensearch',
				search: term,
				format: 'json' }
		dataType: 'jsonp'
	});
}

Now that the Wikipedia Search has been wrapped, we can tie together throttled input and our service call. 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 flatMap method which returns only the latest value.

var suggestions = throttledInput.flatMapLatest( function (text) {
		return searchWikipedia(text);
});

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.

var selector = $('#results');

var subscription = suggestions.subscribe( 
	function (data) {
		selector.clear();
		$.each(data[1], function (_, text) {
			$('<li>' + text + '</li>').appendTo(selector);
		});
	}, 
	function (e) {
		selector.clear();
		$('<li>Error: ' + e + '</li>').appendTo('#results');
	}
);

We've only scratched the surface of this library in this simple example.

Implemented Bindings

Contributing

There are lots of ways to contribute to the project, and we appreciate our contributors.

You can contribute by reviewing and sending feedback on code checkins, suggesting and trying out new features as they are implemented, submit bugs and help us verify fixes as they are checked in, as well as submit code fixes or code contributions of your own. Note that all code submissions will be rigorously reviewed and tested by the Rx Team, and only those that meet an extremely high bar for both quality and design/roadmap appropriateness will be merged into the source.

License

Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. Microsoft Open Technologies would like to thank its contributors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

More Repositories

1

RxJS

The Reactive Extensions for JavaScript
JavaScript
19,515
star
2

rx.angular.js

AngularJS Bindings for RxJS
JavaScript
825
star
3

RxJS-DOM

HTML DOM Bindings for the Reactive Extensions for JavaScript
JavaScript
437
star
4

RxJSKoans

RxJS Koans
JavaScript
373
star
5

rx-node

RxJS Bindings for Node.js and io.js
JavaScript
219
star
6

IxJS

Interactive Extensions for JavaScript
JavaScript
158
star
7

IL2JS

IL to JavaScript Compiler
C#
143
star
8

LevelDB

LevelDB for Windows and .NET
C++
118
star
9

RxPy

The Reactive Extensions for Python
Python
102
star
10

rxjs-node

Reactive Extensions bindings for node.js
JavaScript
70
star
11

FutureJS

Presentation from FutureJS
JavaScript
44
star
12

Reactor

Foundations for a distributed implementation of Rx.
C#
39
star
13

Rx.ObjC

Reactive Extensions for ObjC
Objective-C
29
star
14

rx.disposables

Standalone library for disposables from RxJS
JavaScript
27
star
15

RxJS-CLI

Reactive Extensions for JavaScript (RxJS) Command Line Interface
JavaScript
23
star
16

RxJS-WinJS

The Reactive Extensions for JavaScript bindings for Windows 8 and WinJS
JavaScript
22
star
17

BuildStuffWorkshop

Workshop for BuildStuff.LT 2014
15
star
18

NDC-Oslo-2014

NDC Oslo 2014 Presentation on Democratizing event processing at all scales and platforms with Reactive Extensions (Rx)
JavaScript
14
star
19

rxjs-winjs-sample

Using RxJS for Windows 8
JavaScript
14
star
20

RxToBand

Reactive Extensions (Rx) support for the Microsoft Band
C#
13
star
21

StrangeLoop2013

StrangeLoop 2013 Rx Presentation
JavaScript
12
star
22

RxJS-Combinators

A functional combinator library
JavaScript
11
star
23

rx.schedulers

Standalone implementation of the RxJS schedulers
JavaScript
11
star
24

RxJS-Contrib

The Reactive Extensions for JavaScript Contrib Project
JavaScript
11
star
25

rx.priorityqueue

Standalone Priority Queue from the RxJS library
JavaScript
10
star
26

RxJS-Dojo

Reactive Extensions bindings for the Dojo Toolkit
JavaScript
10
star
27

RxJS-ExtJS

Ext JS Bindings for the Reactive Extensions for JavaScript (RxJS)
JavaScript
9
star
28

Rx-Edge

Reactive Extensions for Edge.js
JavaScript
5
star
29

RxJS-MooTools

MooTools Bindings for the Reactive Extensions for JavaScript (RxJS)
JavaScript
4
star
30

IxJS-ES6

An ES6 version of the Interactive Extensions for JavaScript (IxJS)
JavaScript
3
star
31

RxJS6

2
star
32

Rx-Summit-2013

Reactive Extensions Summit 2013
2
star
33

blog-posts

Blog posts for posting about Rx
1
star
34

RxJSx

An experimental version of the Reactive Extensions
JavaScript
1
star