zoukankan      html  css  js  c++  java
  • [RxJS] Reactive Programming

    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>
  • 相关阅读:
    linq to sql 还没解决的问题
    js暂停函数,想做牛人的菜鸟遇到了问题。
    公司给我拿800/月 干不干啊?ASP.NET程序员
    android の Handler消息传递机制
    android の Activity & View | LayoutInflate
    ActionScript事件侦听addEventListener的参数详解
    java中String的intern方法和equals方法的使用
    java中short,int转换成byte数组及byte数组转换成short,int
    Flex组件中list里面的数据引用
    Flex中设置弹出窗口的弹出效果(alpha值的渐变和scale值的渐变)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5255822.html
Copyright © 2011-2022 走看看