<!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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [RxJS] Reactive Programming - What is RxJS?

    First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [Reactive Programming] RxJS dynamic behavior

    This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...

  9. [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 ...

随机推荐

  1. extJs项目实战

    extjs是因为在公司用到一次,也是公司唯一一个extjs的项目,当时拿到这个需求的时候,我有点懵逼,这他妈的什么鬼,参加工作两年不到的纯小白,没办法,这是工作,必须要完成的.硬着头皮做吧,好在最后弄 ...

  2. iis配置出现的问题及解决

    唯一密钥属性“value”设置…无法添加类型为add 在配置IIS7.5时,会出现 在唯一密钥属性“value”设置为“default.aspx”(或者index.asp等)时,无法添加类型为“add ...

  3. 10个最实用的Linux命令

    收集了一些对于Linux新手最基本但最有用的Linux命令.你完全可以键入这些命令来管理你的服务器.这些命令对于学习vps或服务器管理的新手最为简便.1.List命令 ls -a //列出所有文件 l ...

  4. 初学Java ssh之Spring 第二篇

    上一篇我们成功搭建好了spring开发的环境,接下来一起看看spring有什么神奇的地方吧(本人也是新手,有不太对的地方希望大神给指出便于修改呢,谢谢大家). 之前听说spring是在对xml文件的应 ...

  5. rdf

    更多内容请看这里:http://www.w3school.com.cn/rdf/index.asp 资源描述框架 (RDF) 是描述网络中资源的 W3C 标准. RDF 是一个框架,用来描述网络资源, ...

  6. 转。webapp开发小tips

     备忘 - Q:  webapp点击一个按钮调用系统拨号: <a href="tel:12345654321">打电话给我</a> <a href=& ...

  7. C++第三课(2013.10.03 )

    函数的默认参数: 1.函数的默认参数必须放在形参的右边而且在默认形参的右边不能出现没有无默认参数的形参 2.如果函数的声明给出了默认的参数,那么函数的实现就不能定义默认参 3.声明成员函数时没有给出默 ...

  8. Python新手学习基础之运算符——成员运算与身份运算

    成员运算符 Python里有成员运算符,可以判断一个元素是否在某一个序列中.比如可以判断一个字符是否属于这个字符串,可以判断某个对象是否在这个列表中等等. Python中的成员操作符的使用语法是: o ...

  9. C语言初学 转义字符举例

    #include<stdio.h> main() { printf("\101 \x42 C\n"); printf("I say:\"How ar ...

  10. Java学习笔记--Collection和Collections的区别

    转自 http://pengcqu.iteye.com/blog/492196 比较Collection 和Collections的区别.   1.java.util.Collection 是一个集合 ...