[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 ...
随机推荐
- ExtJs在vs中的应用
目标: 认识EXTJS(自己google) 在vs中创建EXTJS的编程环境 通过一个简单的例子了解EXTJS编程过程 内容: 个人理解EXTJS是一个基于ajax富客户端应用程序框架, 1,创建vs ...
- Cretiria查询应用(一)
1.查询所有 Criteria criteria=session.createCriteria(Dept.class); //调用list()方法 List<Dept> li ...
- (转)WITH (NOLOCK)
缺点: 1.会产生脏读 2.只适用与select查询语句 优点: 1.有些文件说,加了WITH (NOLOCK)的SQL查询效率可以增加33%. 2.可以用于inner join 语句 脏读: 一个用 ...
- C# Socket传输大文件
1.基础类TransferFiles,client和server都需要 using System; using System.Collections.Generic; using System.Tex ...
- Google推出iOS功能性UI测试框架EarlGrey
经过了一段时间的酝酿后,Google很高兴地宣布了EarlGrey,一款针对于iOS的功能性UI测试框架.诸如YouTube.Google Calendar.Google Photos.Google ...
- C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询
1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法 ...
- POI创建Excle
1.导包 2.Demo Workbook wb=new HSSFWorkbook();//创建工作空间 Sheet sh= wb.createSheet("工作表1");//创建工 ...
- hdu5353
模拟,,, 每个人有一些糖果,每两个人之间只能给一个糖果,问最后是否能让所有人的糖果数量相同,只要确定一个糖果的流向其他的就能够确定. 马虎了,卡了好几天,心塞塞的... #include<io ...
- Mac实用技巧
1. 程序员Mac新装机必备 Mac很玄这个大家都承认,但是鄙人觉得程序员用Mac才能真正发挥它的功效.下面就说说我的Mac使用: 基本编程软件:xcode,这个东西不仅仅是对mac的界面程序开发有用 ...
- 理想与现实——观电影《Dead Poets Society》有感
我们每一个人都注定要死去,看看那些旧照片,照片里的年轻人现在都在哪里呢?也许有的人曾经充满活力,曾经信誓旦旦地要去改变这个世界,但如今却变得只知道顺从,如果你去问他们,他们会说:大概这就是现实吧. 现 ...