[RxJS] Reactive Programming - New requests from refresh clicks -- merge()
Now we want each time we click refresh button, we will get new group of users. So we need to get the refresh button click event stream:
var refreshButton = document.querySelector('.refresh');
var refreshClickStream = Rx.Observable.fromEvent(refreshButton, 'click');
Then each time refreshClickStream happens, we will get users request url:
var requestOnRefreshStream = refreshClickStream
.map(ev => {
var randomOffset = Math.floor(Math.random()*500);
return 'https://api.github.com/users?since=' + randomOffset;
});
And we use this url to get json object:
var responseStream = requestOnRefreshStream
.flatMap(requestUrl =>
Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
);
But the problem here is when the page loaded, we haven't click refresh button yet, therefore there is no data fetched fromt the server.
So we need to have a stream when the page loaded:
var startupRequestStream = Rx.Observable.just('https://api.github.com/users');
Then we can merge startUpRequestStream with requestOnRefreshStream:
var responseStream = requestOnRefreshStream
.merge(startupRequestStream)
.flatMap(requestUrl =>
Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
);
//-------a-----b-----c--------->
//s----------------------------->
//merge
//s------a-----b-----c--------->
[RxJS] Reactive Programming - New requests from refresh clicks -- merge()的更多相关文章
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- [Reactive Programming] Async requests and responses in RxJS
We will learn how to perform network requests to a backend using RxJS Observables. A example of basi ...
- [RxJS] Reactive Programming - Sharing network requests with shareReplay()
Currently we show three users in the list, it actually do three time network request, we can verfiy ...
- [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
In currently implemention, there is one problem, when the page load and click refresh button, the us ...
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()
So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...
- [RxJS] Reactive Programming - Why choose RxJS?
RxJS is super when dealing with the dynamic value. Let's see an example which not using RxJS: var a ...
- [Reactive Programming] Using an event stream of double clicks -- buffer()
See a practical example of reactive programming in JavaScript and the DOM. Learn how to detect doubl ...
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
随机推荐
- C++指针数组和数组指针
指针相关问题 using namespace std; int main(){ //a) 一个整型数( An integer) int a; //b) 一个指向整型数的指针( A pointer to ...
- Linux中/etc/passwd文件与/etc/shadow文件解析.
此文章转载自"慧可",用来学习. 1. /etc/passwd文件 1.1 /etc/passwd文件内容格式 用户名: 密码 : uid : gid :用户描述:主目录:登陆s ...
- Hibernate项目里配置环境时,jar包配置不当会对测试结果产生影响。
问题情况: 如下图所示,该图中,显示了一堆错误的jar包.这是导致了Junit4测试通过了,数据库却没反应的原因. 原因: 之所以出现这种情况,eclipse其实已经告诉了我们原因.如下图所示,这些j ...
- jquery之营销系统(会员促销)
var appPath = getAppPath(); var cnt = 0; var loadCnt = 0; $(function() { $("#opreateHtml") ...
- Linux命令初步了解
知识点: 1.虚拟控制台: 在系统启动时直接进入字符工作方式后,系统提供了多个(默认为6个)虚拟控制台.每个虚拟控制台可以相互独立使用,互不影响. 可以使用Alt+F1~Alt+F6进行多个虚拟控制台 ...
- iOS_SN_LLDB常用命令
有一次因为封装一个控件,UI能正常显示就是不能点击,一点击就崩溃,而且异常断点也无法捕捉,把Xcode的僵尸对象打开,每次崩溃就打印一个地址,最后就必须根据地址寻找对象,可以使用frame varia ...
- C++ list用法
创建一个list实例并赋值: // 创建实例以及赋值 #include <iostream> #include <list> using namespace std; int ...
- GridView Footer页脚统计实现多行
在使用GridView时有时会需要多行显示页脚Footer的统计,下面是一种解决方法,仅仅供各位参考 在GridView的RowCreated事件中添加多行页脚,实例代码如下: protected v ...
- MVC中,加入的一个aspx页面用到AspNetPager控件处理办法
今天项目遇到了如题所示的问题,按照官方的案例介绍做分页,简直要奔溃了, 使用URL重写,但是page总是1,根本不跳, 不使用URL重写,又出现,第一页是 http://aa.com/view_asp ...
- px em rem
px 在Web页面初期制作中,我们都是使用“px”来设置我们的文本,因为他比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,他改变了浏览器的字体大小,这时会使用我们 ...