[RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.7.2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.8/rx.all.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div class="container">
<div class="header">
<h3>Who to follow</h3><a href="#" class="refresh">Refresh</a>
</div>
<ul class="suggestions">
<li class="suggestion1">
<img />
<a href="#" target="_blank" class="username">this will not be displayed</a>
<a href="#" class="close close1">x</a>
</li>
<li class="suggestion2">
<img />
<a href="#" target="_blank" class="username">neither this</a>
<a href="#" class="close close2">x</a>
</li>
<li class="suggestion3">
<img />
<a href="#" target="_blank" class="username">nor this</a>
<a href="#" class="close close3">x</a>
</li>
</ul>
</div>
</body>
</html>
var refreshButton = document.querySelector('.refresh');
var requestStream = Rx.Observable.just('https://api.github.com/users');
var responseStream = requestStream
.flatMap(requestUrl =>
Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
);
function createSuggestionStream(responseStream) {
return responseStream.map(listUser =>
listUser[Math.floor(Math.random()*listUser.length)]
);
}
var suggestion1Stream = createSuggestionStream(responseStream);
var suggestion2Stream = createSuggestionStream(responseStream);
var suggestion3Stream = createSuggestionStream(responseStream);
function renderSuggestion(userData, selector) {
var element = document.querySelector(selector);
var usernameEl = element.querySelector('.username');
usernameEl.href = userData.html_url;
usernameEl.textContent = userData.login;
var imgEl = element.querySelector('img');
imgEl.src = userData.avatar_url;
}
suggestion1Stream.subscribe(user => {
renderSuggestion(user, '.suggestion1');
});
suggestion2Stream.subscribe(user => {
renderSuggestion(user, '.suggestion2');
});
suggestion3Stream.subscribe(user => {
renderSuggestion(user, '.suggestion3');
});
body {
font-family: sans-serif;
padding: 10px;
}
h3 {
font-weight: bold;
display: inline-block;
padding:;
margin:;
}
.refresh {
font-size: 80%;
margin-left: 10px;
}
.header {
background: #ECECEC;
padding: 5px;
}
.suggestions {
border: 2px solid #ECECEC;
}
li {
padding: 5px;
}
li img {
width: 40px;
height: 40px;
border-radius: 20px;
}
li .username, li .close {
display: inline-block;
position: relative;
bottom: 15px;
left: 5px;
}
[RxJS] Reactive Programming - Rendering on the DOM with RxJS的更多相关文章
- [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 - 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 - 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 - Why choose RxJS?
RxJS is super when dealing with the dynamic value. Let's see an example which not using RxJS: var a ...
- [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 - 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 ...
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
- [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 ...
随机推荐
- C#调用R语言输出图片
参考:http://rdotnet.codeplex.com/documentation REngine.SetEnvironmentVariables(); REngine engine = R ...
- Mysql数据库里面的String类型依照数字来排序以及按时间排序的sql语句
今天做项目的时候,遇到个小小的问题,在数据库中查询的时候,要用String类型的ID进行一下排序!(注:ID字段为 varchar 类型) 解决的方法: 如: SELECT * FROM Stude ...
- 关于driver_register做了些什么
现在进入driver_register()函数去看看.在driver_register() 中,调用了driver_find(drv->name, drv->bus)函数,这里是干啥呢?这 ...
- openwrt sdk compile
recently ,bought a router : tl-wr741n-v5 hd my aim : let the router dail in neetkeeper environment : ...
- 【回顾整理】HTML+CSS个的两个实战项目
一:麦子商城首页制作 代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset= ...
- NSUserDefaults的使用方法
NSUserDefaults对象是用来保存,恢复应用程序相关的偏好设置,配置数据等等,用户再次打开程序或开机后这些数据仍然存在.默认系统允许应用程序自定义它的行为去迎合用户的喜好.你可以在程序运行的时 ...
- .net抓取网页数据
1.想通过代码获得某个页面的数据,首先根据右键查看页面源代码,通过分析.再通过下面代码,修改,一步步查找出所需内容,存入数据库. //根据Url地址得到网页的html源码 private string ...
- 不用标准库实现memmove,借助一个缓冲区temp,即使src和dest所指的内存有重叠也能正确拷贝
void *mymemmove(void *dest,const void *src,size_t count) { char temp[count]; int i; char *d=(char *) ...
- jQuery select 操作全集
添加option $('#id').append("<option value="value">Text</option>");//为s ...
- MySQL按照汉字的拼音排序(转)
按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...