[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 user list is not immediatly clean up,it is cleared after the new data come in.
So two things we need to do,
1. Hide User Elements when the user object is null
2. clean the list when page first load
3. clean the list after each refresh click
1. In the renderSuggestion method, check the suggestedUser is null or not, if it is null, then we hide the user item.
function renderSuggestion(suggestedUser, selector) {
var suggestionEl = document.querySelector(selector);
if (suggestedUser === null) {
suggestionEl.style.visibility = 'hidden';
} else {
suggestionEl.style.visibility = 'visible';
var usernameEl = suggestionEl.querySelector('.username');
usernameEl.href = suggestedUser.html_url;
usernameEl.textContent = suggestedUser.login;
var imgEl = suggestionEl.querySelector('img');
imgEl.src = "";
imgEl.src = suggestedUser.avatar_url;
}
}
2. So when the page load, you can see the frash, because we have placeholder in the html:
<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>
When the data comes in, it will be replaced with data, it will be obvious when the low speed internet.
SO in the createSuggestion method, we use startWith(null), it set suggestionUser to null:
function createSuggestionStream(responseStream) {
return responseStream.map(listUser =>
listUser[Math.floor(Math.random()*listUser.length)]
)
.startWith(null);
}
This will solve the problem when first loading, the user list frashing...
3. In each responseStream, we merge with refreshClickStream and return null value. Because network request take time to finish, so the null value will effect the responseStream first, so the user item can be hided.
function createSuggestionStream(responseStream) {
return responseStream.map(listUser =>
listUser[Math.floor(Math.random()*listUser.length)]
)
.startWith(null)
.merge(refreshClickStream.map(ev => null));
}
//----r---------------------r---------->
// startWith(N)
//N---r------------------------------->
//------------------N-----N----------->
// merge
//N---r-----------------r---r---------->
//
--------------------------
var refreshButton = document.querySelector('.refresh');
var refreshClickStream = Rx.Observable.fromEvent(refreshButton, 'click');
var startupRequestStream = Rx.Observable.just('https://api.github.com/users');
var requestOnRefreshStream = refreshClickStream
.map(ev => {
var randomOffset = Math.floor(Math.random()*500);
return 'https://api.github.com/users?since=' + randomOffset;
});
var responseStream = startupRequestStream.merge(requestOnRefreshStream)
.flatMap(requestUrl =>
Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
);
function createSuggestionStream(responseStream) {
return responseStream.map(listUser =>
listUser[Math.floor(Math.random()*listUser.length)]
)
.startWith(null)
.merge(refreshClickStream.map(ev => null));
}
var suggestion1Stream = createSuggestionStream(responseStream);
var suggestion2Stream = createSuggestionStream(responseStream);
var suggestion3Stream = createSuggestionStream(responseStream);
// Rendering ---------------------------------------------------
function renderSuggestion(suggestedUser, selector) {
var suggestionEl = document.querySelector(selector);
if (suggestedUser === null) {
suggestionEl.style.visibility = 'hidden';
} else {
suggestionEl.style.visibility = 'visible';
var usernameEl = suggestionEl.querySelector('.username');
usernameEl.href = suggestedUser.html_url;
usernameEl.textContent = suggestedUser.login;
var imgEl = suggestionEl.querySelector('img');
imgEl.src = "";
imgEl.src = suggestedUser.avatar_url;
}
}
suggestion1Stream.subscribe(user => {
renderSuggestion(user, '.suggestion1');
});
suggestion2Stream.subscribe(user => {
renderSuggestion(user, '.suggestion2');
});
suggestion3Stream.subscribe(user => {
renderSuggestion(user, '.suggestion3');
});
<!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>
[RxJS] Reactive Programming - Clear data while loading with RxJS startWith()的更多相关文章
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [Vue-rx] Disable Buttons While Data is Loading with RxJS and Vue.js
Streams give you the power to handle a "pending" state where you've made a request for dat ...
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- [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 ...
- [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 - 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 ...
- [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 ...
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
随机推荐
- Codeforces 328A-IQ Test(数列)
A. IQ Test time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- mvc中使用jsonp进行跨域请求详细说明
在web开发中,如果你要在不同域下进行数据异步请求,会出现一个No ‘Access-Control-Allow-Origin’ header is present on the requested r ...
- F# 可以把几个函数组合成新函数
C#能做的,F#基本都能做,但F#能做的,C#未必能做. F#中的函数可以把几个函数组合起来使用.下面的例子是把由 function1 和 function2 这两个函数通过运算符“>>” ...
- $(document).ready(); $().ready(); $()
$(document).ready(function(){}); $().ready(function(){}); $(function(){}), 三者效果是一样的,在文档加载完成之后执行()中的代 ...
- Ring对象
Ring是一个封闭的Path即起始和终止点有相同的坐标值,它有内部和外部属性.
- SignalR小计
微软官方例子地址:http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr-a ...
- (原)ubuntu14.04中安装gcc4.9和g++4.9
http://stackoverflow.com/questions/28683747/installing-gcc4-9-on-ubuntu-14-04-lts http://askubuntu.c ...
- debug连线指令
-當發現原本正常的連線突然斷線的,請用底下的指令debug 1.ifconfig (看看IPaddress) 2.wpa_clira stat (看看連線狀態) 3.如果是在連線中(wp ...
- spark1.3.1使用基础教程
spark可以通过交互式命令行及编程两种方式来进行调用: 前者支持scala与python 后者支持scala.python与java 本文参考https://spark.apache.org/doc ...
- js中的逻辑或和逻辑与
a=''||'abc'; //返回什么? 'abc' a=1||2; // ...