zoukankan      html  css  js  c++  java
  • js异步调用导致的代码不雅

    先上一段代码:

           // add event to delete button
                on(deleteButton, 'click', lang.hitch(this, function() {
                    // avoid duplicate click
                    deleteButton.set('disabled', true);
    
                    // show message box
                    MessageBox.confirm('Are you sure to delete?', lang.hitch(this, function(userChoice) {
                        if (userChoice) {
                            // delete
                            // call service to delete
                            var putResult = new ModalityService().remove(_this.modalityInfo.device.id);
                            putResult.then(function(data) {
    
                                if (data.code == 0) {
                                    router.goId('modality_list');
                                } else if (data.code == 200) {
                                    // TODO enable delete button
                                }
    
                            });
                        } else {
                            // TODO enable delete button.
                        }
                    }));
    
                }));

    场景:删除之前disable删除按钮,操作成功返回列表,失败,按钮重新enable。

    让事情变复杂的是:Javascript的异步调用,不能只在最后enable一下删除按钮。

    所有,有没有更优雅的方式来解决这个问题。

    好像还真没有,顶多封装封装函数,但是本质没变。

  • 相关阅读:
    String.format in JavaScript
    dojo/domReady! 中感叹号的作用
    文本三剑客之sed的用法
    文本三剑客之grep的用法
    通配符与特殊符号
    文件属性信息
    文件属性及find命令总结
    linux系统常用命令
    系统优化
    vim编辑器 与etc目录
  • 原文地址:https://www.cnblogs.com/evanxyhu/p/js-async-call-cause-code-not-beautiful.html
Copyright © 2011-2022 走看看