zoukankan      html  css  js  c++  java
  • 重复代码,应封装为函数进行调用

    // bad case 都有展示modal的逻辑,开发同学直接复制粘贴
    function handleA(msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#02BACC',
        success: (res) => {
          if (res.confirm) {
            doA();
           }
         },
      });
    }

    function handleB(msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#02BACC',
        success: (res) => {
          if (res.confirm) {
            doB();
           }
         },
      });
    }

    function handleC(msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#02BACC',
        success: (res) => {
          if (res.confirm) {
            doC();
           }
         },
      });
    }

    解决方案,封装 showModal 函数。

    function showModal (msg) {
      return new Promise((resolve, reject) => {
        wx.showModal({
          title: '提示',
          content: msg,
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#02BACC',
          success: (res) => {
            if (res.confirm) resolve()
          },
          fail: (err) => {
            reject(err)
          }
        })
      })
    }

    funtion handleA(msg) {
      showModal(msg).then(
        doA();
      ).catch(() => { catchHandler();})
    }

    funtion handleB(msg) {
      showModal(msg).then(
        doB();
      ).catch(() => { catchHandler();})
    }

    funtion handleC(msg) {
      showModal(msg).then(
        doC();
      ).catch(() => { catchHandler();})
    }
  • 相关阅读:
    STL杂记
    python实现进度条
    python socket 传输文件
    python实现生产者消费者模型
    python之线程和进程(并发编程)
    进程与线程
    python常用模块二正则表达式
    Python中的库使用之一 PIL
    Vmware中Ubuntu的各种问题
    Python当中的正则表达式支持!
  • 原文地址:https://www.cnblogs.com/hellofangfang/p/15206064.html
Copyright © 2011-2022 走看看