zoukankan      html  css  js  c++  java
  • [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 data, but the data hasn't yet returned. You can leverage this pending stream to update your template as well which is often displayed as a "loader" or disabling some part of the UI.

    <template>
      <section class="section">
        <button class="button" :disabled="disabled$" v-stream:click="click$">{{buttonText$}}</button>
        <h2>
          {{name$}}
        </h2>
        <img v-stream:error="imageError$" :src="image$" alt="">
      </section>
    </template>
    
    <script>
    import { from, of, merge, throwError } from 'rxjs';
    import {
      switchMap,
      pluck,
      map,
      mapTo,
      catchError,
      shareReplay,
      share,
      startWith
    } from 'rxjs/operators';
    
    export default {
      name: 'app',
      domStreams: ['click$', 'imageError$'],
      subscriptions() {
        const createLoader = url => from(this.$http.get(url)).pipe(pluck('data'));
    
        const luke$ = this.click$.pipe(
          mapTo('https://starwars.egghead.training/people/1'),
          switchMap(createLoader),
          catchError(() => of({name: 'Failed.. :('})),
          share()
        );
    
        const name$ = luke$.pipe(pluck('name'));
    
        const loadImage$ = luke$.pipe(
          pluck('image'),
          map(src => `https://starwars.egghead.training/${src}`)
        );
    
        const failImage$ = this.imageError$.pipe(
          mapTo(`http://via.placeholder.com/400x400`)
        );
    
        const image$ = merge(
          loadImage$,
          failImage$
        )
    
        const disabled$ = merge(
          this.click$.pipe(mapTo(true)),
          luke$.pipe(mapTo(false)),
        ).pipe(startWith(false));
    
        const buttonText$ = disabled$.pipe(map(b => (b ? 'Loading...' : 'Load')));
    
    
        return {
          name$,
          image$,
          failImage$,
          disabled$,
          buttonText$
        };
      }
    };
    </script>
  • 相关阅读:
    CodeForces 733B Parade
    LeetCode 150 Evaluate Reverse Polish Notation
    LeetCode 148 Sort List
    数据库的迁移
    LeetCode 147. Insertion Sort List
    构建一个可以统计 qps 的nginx服务的Dockerfile
    各城市区号
    tkinter中menu菜单控件(十二)
    tkinter中scale拖拉改变值控件(十一)
    tkinter中spinbox递增和递减控件(十)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9332241.html
Copyright © 2011-2022 走看看