You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 20, 2018. It is now read-only.
Copy file name to clipboardExpand all lines: nuget/RxJS-Bridges-jQuery/RxJS-Bridges-jQuery.nuspec
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
<id>RxJS-Bridges-jQuery</id>
5
5
<title>Reactive Extensions for JavaScript - Bridges to jQuery </title>
6
6
<!-- Automatically updated by build, keeping fixed dev build number here in case of local build -->
7
-
<version>1.1.3</version>
7
+
<version>1.1.4</version>
8
8
<authors>Microsoft Corporation</authors>
9
9
<description>This project provides Reactive Extensions for JavaScript (RxJS) bindings for jQuery to abstract over the event binding, Ajax and Deferreds.</description>
@@ -27,7 +27,7 @@ To download the source of the jQuery Bindings for the Reactive Extensions for Ja
27
27
28
28
jam install rx-jquery
29
29
30
-
### Installing with NuGet
30
+
### Installing with [NuGet](http://nuget.org)
31
31
32
32
PM> Install-Package RxJS-Bridges-jQuery
33
33
@@ -38,8 +38,9 @@ Let's walk through a simple yet powerful example of the Reactive Extensions for
38
38
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.
@@ -53,136 +54,140 @@ We'll start out with a basic skeleton for our application with script references
53
54
54
55
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.
55
56
56
-
var throttledInput = $('#textInput')
57
-
.keyupAsObservable()
58
-
59
-
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.
60
-
61
-
.select( function (ev) {
62
-
return $(ev.target).val();
63
-
})
64
-
65
-
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.
66
-
67
-
.where( function (text) {
68
-
return text.length > 2;
69
-
})
70
-
71
-
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.
72
-
73
-
.throttle(500)
74
-
57
+
```js
58
+
var throttledInput =$('#textInput')
59
+
.keyupAsObservable()
60
+
```
61
+
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.
62
+
```js
63
+
.map( function (ev) {
64
+
return$(ev.target).val();
65
+
})
66
+
```
67
+
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.
68
+
```js
69
+
.filter( function (text) {
70
+
returntext.length>2;
71
+
})
72
+
```
73
+
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.
74
+
```js
75
+
.throttle(500/* ms */)
76
+
```
75
77
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.
76
-
77
-
.distinctUntilChanged();
78
-
78
+
```js
79
+
.distinctUntilChanged();
80
+
```
79
81
Putting it all together, our throttledInput looks like the following:
80
-
81
-
var throttledInput = $('#textInput')
82
-
.keyupAsObservable()
83
-
.select( function (ev) {
84
-
return $(ev.target).val();
85
-
})
86
-
.where( function (text) {
87
-
return text.length > 2;
88
-
})
89
-
.throttle(500)
90
-
.distinctUntilChanged();
91
-
92
-
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).
93
-
94
-
function searchWikipedia(term) {
95
-
return $.ajaxAsObservable({
96
-
url: 'http://en.wikipedia.org/w/api.php',
97
-
data: { action: 'opensearch',
98
-
search: term,
99
-
format: 'json' }
100
-
dataType: 'jsonp'
101
-
});
102
-
}
103
-
104
-
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 [switchLatest](http://msdn.microsoft.com/en-us/library/hh229197\(v=VS.103\).aspx) method which returns only the latest value.
105
-
106
-
var suggestions = throttledInput.select( function (text) {
107
-
return searchWikipedia(text);
82
+
```js
83
+
var throttledInput =$('#textInput')
84
+
.keyupAsObservable()
85
+
.map( function (ev) {
86
+
return$(ev.target).val();
108
87
})
109
-
.switchLatest();
110
-
88
+
.filter( function (text) {
89
+
returntext.length>2;
90
+
})
91
+
.throttle(500)
92
+
.distinctUntilChanged();
93
+
```
94
+
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`.
95
+
```js
96
+
functionsearchWikipedia(term) {
97
+
return$.ajaxAsObservable({
98
+
url:'http://en.wikipedia.org/w/api.php',
99
+
data: { action:'opensearch',
100
+
search: term,
101
+
format:'json' }
102
+
dataType:'jsonp'
103
+
});
104
+
}
105
+
```
106
+
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.
107
+
```js
108
+
var suggestions =throttledInput.flatMapLatest( function (text) {
109
+
returnsearchWikipedia(text);
110
+
});
111
+
```
111
112
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.
113
+
```js
114
+
var selector =$('#results');
112
115
113
-
suggestions.subscribe( function (data) {
114
-
var selector = $('#results');
116
+
var subscription =suggestions.subscribe(
117
+
function (data) {
115
118
selector.clear();
116
119
$.each(data[1], function (_, text) {
117
120
$('<li>'+ text +'</li>').appendTo(selector);
118
121
});
119
-
}, function (e) {
122
+
},
123
+
function (e) {
120
124
selector.clear();
121
125
$('<li>Error: '+ e +'</li>').appendTo('#results');
122
-
});
123
-
126
+
}
127
+
);
128
+
```
124
129
We've only scratched the surface of this library in this simple example.
0 commit comments