Installations
npm install rx-lite-dom-ajax
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
4.2.0
NPM Version
2.14.7
Statistics
437 Stars
244 Commits
99 Forks
23 Watching
2 Branches
42 Contributors
Updated on 25 Oct 2024
Bundle Size
86.01 kB
Minified
19.66 kB
Minified + Gzipped
Languages
JavaScript (99.02%)
HTML (0.98%)
Total Downloads
Cumulative downloads
Total Downloads
16,513
Last day
0%
637
Compared to previous day
Last week
36.9%
1,339
Compared to previous week
Last month
5,636.6%
2,352
Compared to previous month
Last year
220.7%
2,745
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
RxJS-DOM 7.0 - HTML DOM Bindings for the Reactive Extensions for JavaScript
OVERVIEW
This project provides Reactive Extensions for JavaScript (RxJS) bindings for HTML DOM objects to abstract over the event binding, Ajax requests, Web Sockets, Web Workers, Server-Sent Events, Geolocation and more.
Batteries Included
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:
Main Libraries:
rx.dom.js
rx.dom.compat.js
rx.dom.ajax.js
rx.dom.ajax.compat.js
rx.dom.concurrency.js
rx.dom.events.js
rx.dom.events.compat.js
rx.dom.html.js
Lite Libraries:
rx.lite.dom.js
rx.lite.dom.compat.js
rx.lite.dom.ajax.js
rx.lite.dom.ajax.compat.js
rx.lite.dom.concurrency.js
rx.lite.dom.events.js
rx.lite.dom.events.compat.js
rx.lite.dom.html.js
GETTING STARTED
There are a number of ways to get started with the HTML DOM Bindings for RxJS. The files are available on cdnjs and jsDelivr.
Download the Source
To download the source of the HTML DOM Bindings for the Reactive Extensions for JavaScript, type in the following:
1git clone https://github.com/Reactive-Extensions/rxjs-dom.git 2cd ./rxjs-dom
Installing with NPM
1npm install rx-dom
Installing with Bower
1bower install rx-dom
Installing with Jam
1jam install rx-dom
Installing with NuGet
1PM> Install-Package RxJS-Bridges-HTML
Getting Started with the HTML DOM Bindings
Let's walk through a simple yet powerful example of the Reactive Extensions for JavaScript Bindings for HTML, 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 RxJS Lite based methods, and the RxJS Bindings for HTML DOM, along with a textbox for input and a list for our results.
1<script type="text/javascript" src="rx.lite.js"></script> 2<script type="text/javascript" src="rx.dom.js"></script> 3<script type="text/javascript"> 4 5</script> 6... 7<input id="textInput" type="text"></input> 8<ul id="results"></ul> 9...
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.
1var textInput = document.querySelector('#textInput'); 2var throttledInput = Rx.DOM.keyup(textInput);
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.
1 .pluck('target','value')
We're also not interested in query terms less than two letters, so we'll trim that user input by using the where
or filter
method returning whether the string length is appropriate.
1 .filter( function (text) { 2 return text.length > 2; 3 })
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.
1 .debounce(500)
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 distinctUntilChanged
method.
1 .distinctUntilChanged();
Putting it all together, our throttledInput looks like the following:
1var textInput = document.querySelector('#textInput'); 2var throttledInput = Rx.DOM.keyup(textInput) 3 .pluck('target','value') 4 .filter( function (text) { 5 return text.length > 2; 6 }) 7 .debounce(500) 8 .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 Rx.DOM.jsonpRequest
method which wraps making a JSONP call.
1function searchWikipedia(term) {
2 var url = 'http://en.wikipedia.org/w/api.php?action=opensearch&format=json&search='
3 + encodeURIComponent(term) + '&callback=JSONPCallback';
4 return Rx.DOM.jsonpRequest(url);
5}
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.
1var suggestions = throttledInput.flatMapLatest(searchWikipedia);
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.
1var resultList = document.getElementById('results'); 2 3function clearSelector (element) { 4 while (element.firstChild) { 5 element.removeChild(element.firstChild); 6 } 7} 8 9function createLineItem(text) { 10 var li = document.createElement('li'); 11 li.innerHTML = text; 12 return li; 13} 14 15suggestions.subscribe( 16 function (data) { 17 var results = data.response[1]; 18 19 clearSelector(resultList); 20 21 for (var i = 0; i < results.length; i++) { 22 resultList.appendChild(createLineItem(results[i])); 23 } 24 }, 25 function (e) { 26 clearSelector(resultList); 27 resultList.appendChild(createLineItem('Error: ' + e)); 28 } 29); 30
We've only scratched the surface of this library in this simple example.
Dive In!
Please check out:
- Our Code of Conduct
- The full documentation
- Our many great examples
- Our design guidelines
- Our contribution guidelines
- Our complete Unit Tests
LICENSE
Copyright Microsoft
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.
No vulnerabilities found.
Reason
0 existing vulnerabilities detected
Reason
binaries present in source code
Details
- Warn: binary detected: .nuget/nuget.exe:1
Reason
license file detected
Details
- Info: project has a license file: license.txt:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 8/27 approved changesets -- score normalized to 2
Reason
project is archived
Details
- Warn: Repository is archived.
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Score
3.1
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to rx-lite-dom-ajax
rx-lite
Lightweight library for composing asynchronous and event-based operations in JavaScript
rx-lite-aggregates
Lightweight library with aggregate functions for composing asynchronous and event-based operations in JavaScript
@types/rx
TypeScript definitions for rx
@types/rx-lite-experimental
TypeScript definitions for rx-lite-experimental