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.

  1. function renderSuggestion(suggestedUser, selector) {
  2. var suggestionEl = document.querySelector(selector);
  3. if (suggestedUser === null) {
  4. suggestionEl.style.visibility = 'hidden';
  5. } else {
  6. suggestionEl.style.visibility = 'visible';
  7. var usernameEl = suggestionEl.querySelector('.username');
  8. usernameEl.href = suggestedUser.html_url;
  9. usernameEl.textContent = suggestedUser.login;
  10. var imgEl = suggestionEl.querySelector('img');
  11. imgEl.src = "";
  12. imgEl.src = suggestedUser.avatar_url;
  13. }
  14. }

2. So when the page load, you can see the frash, because we have placeholder in the html:

  1. <ul class="suggestions">
  2. <li class="suggestion1">
  3. <img />
  4. <a href="#" target="_blank" class="username">this will not be displayed</a>
  5. <a href="#" class="close close1">x</a>
  6. </li>
  7. <li class="suggestion2">
  8. <img />
  9. <a href="#" target="_blank" class="username">neither this</a>
  10. <a href="#" class="close close2">x</a>
  11. </li>
  12. <li class="suggestion3">
  13. <img />
  14. <a href="#" target="_blank" class="username">nor this</a>
  15. <a href="#" class="close close3">x</a>
  16. </li>
  17. </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:

  1. function createSuggestionStream(responseStream) {
  2. return responseStream.map(listUser =>
  3. listUser[Math.floor(Math.random()*listUser.length)]
  4. )
  5. .startWith(null);
  6. }

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.

  1. function createSuggestionStream(responseStream) {
  2. return responseStream.map(listUser =>
  3. listUser[Math.floor(Math.random()*listUser.length)]
  4. )
  5. .startWith(null)
  6. .merge(refreshClickStream.map(ev => null));
  7. }
  1. //----r---------------------r---------->
  2. // startWith(N)
  3. //N---r------------------------------->
  4. //------------------N-----N----------->
  5. // merge
  6. //N---r-----------------r---r---------->
  7. //

--------------------------

  1. var refreshButton = document.querySelector('.refresh');
  2.  
  3. var refreshClickStream = Rx.Observable.fromEvent(refreshButton, 'click');
  4.  
  5. var startupRequestStream = Rx.Observable.just('https://api.github.com/users');
  6.  
  7. var requestOnRefreshStream = refreshClickStream
  8. .map(ev => {
  9. var randomOffset = Math.floor(Math.random()*500);
  10. return 'https://api.github.com/users?since=' + randomOffset;
  11. });
  12.  
  13. var responseStream = startupRequestStream.merge(requestOnRefreshStream)
  14. .flatMap(requestUrl =>
  15. Rx.Observable.fromPromise(jQuery.getJSON(requestUrl))
  16. );
  17.  
  18. function createSuggestionStream(responseStream) {
  19. return responseStream.map(listUser =>
  20. listUser[Math.floor(Math.random()*listUser.length)]
  21. )
  22. .startWith(null)
  23. .merge(refreshClickStream.map(ev => null));
  24. }
  25.  
  26. var suggestion1Stream = createSuggestionStream(responseStream);
  27. var suggestion2Stream = createSuggestionStream(responseStream);
  28. var suggestion3Stream = createSuggestionStream(responseStream);
  29.  
  30. // Rendering ---------------------------------------------------
  31. function renderSuggestion(suggestedUser, selector) {
  32. var suggestionEl = document.querySelector(selector);
  33. if (suggestedUser === null) {
  34. suggestionEl.style.visibility = 'hidden';
  35. } else {
  36. suggestionEl.style.visibility = 'visible';
  37. var usernameEl = suggestionEl.querySelector('.username');
  38. usernameEl.href = suggestedUser.html_url;
  39. usernameEl.textContent = suggestedUser.login;
  40. var imgEl = suggestionEl.querySelector('img');
  41. imgEl.src = "";
  42. imgEl.src = suggestedUser.avatar_url;
  43. }
  44. }
  45.  
  46. suggestion1Stream.subscribe(user => {
  47. renderSuggestion(user, '.suggestion1');
  48. });
  49.  
  50. suggestion2Stream.subscribe(user => {
  51. renderSuggestion(user, '.suggestion2');
  52. });
  53.  
  54. suggestion3Stream.subscribe(user => {
  55. renderSuggestion(user, '.suggestion3');
  56. });
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="https://code.jquery.com/jquery-1.7.2.js"></script>
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.8/rx.all.js"></script>
  6. <meta charset="utf-8">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10. <div class="container">
  11. <div class="header">
  12. <h3>Who to follow</h3><a href="#" class="refresh">Refresh</a>
  13. </div>
  14. <ul class="suggestions">
  15. <li class="suggestion1">
  16. <img />
  17. <a href="#" target="_blank" class="username">this will not be displayed</a>
  18. <a href="#" class="close close1">x</a>
  19. </li>
  20. <li class="suggestion2">
  21. <img />
  22. <a href="#" target="_blank" class="username">neither this</a>
  23. <a href="#" class="close close2">x</a>
  24. </li>
  25. <li class="suggestion3">
  26. <img />
  27. <a href="#" target="_blank" class="username">nor this</a>
  28. <a href="#" class="close close3">x</a>
  29. </li>
  30. </ul>
  31. </div>
  32. </body>
  33. </html>

[RxJS] Reactive Programming - Clear data while loading with RxJS startWith()的更多相关文章

  1. [RxJS] Reactive Programming - Rendering on the DOM with RxJS

    <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...

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

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

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

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

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

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

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

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

  9. [Reactive Programming] RxJS dynamic behavior

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

随机推荐

  1. MySQL加密的性能测试

    这是对MySQL进行加密性能测试的两篇文章系列之二.在第一篇中,我专门使用MySQL的内置的对SSL的支持来 做压力测试,产生了一些令人惊讶的结果. AD:WOT2015 互联网运维与开发者大会 热销 ...

  2. HTML基础总结<头部>

    重点摘录:HTML head 元素 标签 描述 <head> 定义了文档的信息 <title> 定义了文档的标题 <base> 定义了页面链接标签的默认链接地址 & ...

  3. java 连接sql server2008配置

    Java 应用程序连接SQL Server2008 (Eclipse+JDK7.0+jdbc4.0.jar+Sql Server2008) 假设应用端的连接语句为: String url = &quo ...

  4. Linux下的静态IP配置【weber出品】

    配置Linux下的静态IP地址 因为服务器的IP地址是固定的,不像我们自己家的笔记本的IP是动态的.所以我们要将这个地址给写成静态的. 直接编辑这个这个配置文件即可: vi /etc/sysconfi ...

  5. 清北学堂 Pa

    PA[题目描述]汉诺塔升级了:现在我们有?个圆盘和?个柱子,每个圆盘大小都不一样,大的圆盘不能放在小的圆盘上面,?个柱子从左到右排成一排.每次你可以将一个柱子上的最上面的圆盘移动到右边或者左边的柱子上 ...

  6. C#实现窗体间的通信

    以下将窗体间的几种通信实现方式做一下罗列:首先新建一个窗体Form1,在其中放置一个Textbox.Button控件.再新建一个窗体Form2,其上放置一个Button控件.具体代码示例如下: //F ...

  7. Java学习笔记--PriorityQueue(优先队列)(堆)

    PriorityQueue(优先队列)实际上是一个堆(不指定Comparator时默认为最小堆)队列既可以根据元素的自然顺序来排序,也可以根据 Comparator来设置排序规则.队列的头是按指定排序 ...

  8. 汉诺塔问题C++实现

    大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下进行汉诺塔问题的递归实现 #include <iostream.h> int gb ...

  9. logstash nginx 报ArgumentError: comparison of String with 5 failed

    80.82.78.38 [23/Sep/2016:05:36:18 +0800] "GET http://www.baidu.com/cache/global/img/gs.gif HTTP ...

  10. MVC4.0 Controller和View重复加载

    项目完成以后总是感觉有一些页面跑起来特别的慢,就仔细的调试了下,发现有很多也买年都是走了两遍,页面加载的时候Controller和View会连续走了两次,没有一点缘由 查了很久也不知道什么原因,这个问 ...