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();})
    }
  • 相关阅读:
    前端
    Spring AOP知识点整理
    【转载】spring aop 面试考点
    【转载】MDC 是什么?
    【转载】在分布式项目中,每个服务器的日志从产生,到集中到一个统一日志平台的流程是什么,中间都有那些过程?
    【转载】门面日志如何自动发现日志组件
    【转载】ArrayList从源码看扩容实现
    【原创】Ajax实现方式
    【转载】servlet与springMVC的差别
    【转载】serlvet
  • 原文地址:https://www.cnblogs.com/hellofangfang/p/15206064.html
Copyright © 2011-2022 走看看