Library for using DOM elements as well as Ajax requests
Sure, there are a lot of libraries to get started with the RxJS bindings for the HTML DOM. Confused on where to get started? Start out with the complete set of functionality with
rx.dom.js, then you can reduce it to the functionality you require such as only events, or ajax. If you use RxJS Lite, you can start with the
rx.lite.dom.js file and then select the functionality you want from there.
This set of libraries include:
git clonecd ./rxjs-dom
npm install rx-dom
bower install rx-dom
jam install rx-dom
PM> Install-Package RxJS-Bridges-HTML
We'll start out with a basic skeleton for our application with script references to RxJS Lite based methods, and the RxJS Bindings for HTML DOM, along with a textbox for input and a list for our results.
The goal here is to take the input from our textbox and debounce 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 the document.getElementById method, then bind to the 'keyup' event using the
Rx.DOM.fromEvent specialization shortcut for keyups called
Rx.DOM.keyup which then takes the DOM element event handler and transforms it into an RxJS Observable.
var textInput = documentquerySelector'#textInput';var throttledInput = RxDOMkeyuptextInput;
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, or we can call
pluck to the same effect.
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.
filterreturn textlength > 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
debounce 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.
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 using the
Putting it all together, our throttledInput looks like the following:
var textInput = documentquerySelector'#textInput';var throttledInput = RxDOMkeyuptextInputpluck'target''value'filterreturn textlength > 2;debounce500distinctUntilChanged;
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
Rx.DOM.jsonpRequest method which wraps making a JSONP call.
var url = ''+ encodeURIComponentterm + '&callback=JSONPCallback';return RxDOMjsonpRequesturl;
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
flatMapLatest method which returns only the latest value.
var suggestions = throttledInputflatMapLatestsearchWikipedia;
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 resultList = documentgetElementById'results';while elementfirstChildelementremoveChildelementfirstChild;var li = documentcreateElement'li';liinnerHTML = text;return li;suggestionssubscribevar results = dataresponse1;clearSelectorresultList;for var i = 0; i < resultslength; i++resultListappendChildcreateLineItemresultsi;clearSelectorresultList;resultListappendChildcreateLineItem'Error: ' + e;;
We've only scratched the surface of this library in this simple example.
Please check out:
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.