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: readme.md
+8-7Lines changed: 8 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,19 +44,19 @@ The goal here is to take the input from our textbox and throttle it in a way tha
44
44
var throttledInput = $('#textInput')
45
45
.keyupAsObservable()
46
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.
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
48
49
49
.select( function (ev) {
50
50
return $(ev.target).val();
51
51
})
52
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.
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
54
55
55
.where( function (text) {
56
56
return text.length > 2;
57
57
})
58
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.
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
60
61
61
.throttle(500)
62
62
@@ -77,7 +77,7 @@ Putting it all together, our throttledInput looks like the following:
77
77
.throttle(500)
78
78
.distinctUntilChanged();
79
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)).
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
81
82
82
function searchWikipedia(term) {
83
83
return $.ajaxAsObservable({
@@ -89,7 +89,7 @@ Now that we have the throttled input from the textbox, we need to query our serv
89
89
});
90
90
}
91
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.
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
93
94
94
var suggestions = throttledInput.select( function (text) {
95
95
return searchWikipedia(text);
@@ -139,6 +139,7 @@ We've only scratched the surface of this library in this simple example.
0 commit comments