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();})
    }
  • 相关阅读:
    go语言xrom使用
    go语言算法
    go语言递归
    go语言map(字典)
    GO语言实现小技巧
    偶遇递归树
    Python中字典对象根据字典某一个key和values去重
    python中将字符串格式转字典
    Azure媒体服务的Apple FairPlay流功能正式上线
    SVG裁剪和平移的顺序
  • 原文地址:https://www.cnblogs.com/hellofangfang/p/15206064.html
Copyright © 2011-2022 走看看