在接收确认对话框响应之前,如何实现等待机制?

时间:2017-05-27 06:03:47

标签: javascript jquery html jquery-ui

这是我的代码:

    $(function () {
        $(document).on('click', '.fa-times', function(e){
            e.stopPropagation();

            var result = ConfirmDialog('Are you sure?');
            if (result) {return false}

            var row = $(this).closest('tr');
            row.css('opacity','0.3');
            var word = $(this).closest('td').siblings('.word').text();
            $.ajax({
                url :  '../../te_addStopWord',
                type : 'GET',
                data: {word : word},
                dataType : 'JSON',
                success : function () {
                    row.remove();
                }, // success
                error : function ($a,$b,$c) {
                    alert($b);
                    row.css('opacity','1');
                }
            }); // ajax
        })


        function ConfirmDialog(message) {
            $('<div></div>').appendTo('body')
                    .html('<div><h6>'+message+'?</h6></div>')
                    .dialog({
                        modal: true, title: 'Delete message', zIndex: 10000, autoOpen: true,
                        width: 'auto', resizable: false,
                        buttons: {
                            Yes: function () {
                                $(this).remove();
                                return true;
                            },
                            No: function () {
                                $(this).remove();
                                return false;
                            }
                        },
                        close: function (event, ui) {
                            $(this).remove();
                        }
                    });
        };


    })

但我的代码运行该ajax请求而不等待ConfirmDialog(message)函数的响应。我该怎么强迫它等?

2 个答案:

答案 0 :(得分:1)

根据选择的确认按钮,您有2个触发的功能。从那里处理你的ajax代码。

我喜欢Abhijit的答案如何抽象出代码,但我认为他错过了一些东西。

$(function() {
  $(document).on('click', '.fa-times', function(e) {
    e.stopPropagation();

    function getConfirmHandler(element){
      return function handleConfirm(result) {
          if (result) {
            return false
          }

          var row = element.closest('tr');
          row.css('opacity', '0.3');
          var word = element.closest('td').siblings('.word').text();
          $.ajax({
            url: '../../te_addStopWord',
            type: 'GET',
            data: {
              word: word
            },
            dataType: 'JSON',
            success: function() {
              row.remove();
            }, // success
            error: function($a, $b, $c) {
              alert($b);
              row.css('opacity', '1');
            }
          }); // ajax
      }
    }

    ConfirmDialog('Are you sure?', getConfirmHandler($(this)))
  });


  function ConfirmDialog(message, callback) {
    $('<div></div>').appendTo('body')
      .html('<div><h6>' + message + '?</h6></div>')
      .dialog({
        modal: true,
        title: 'Delete message',
        zIndex: 10000,
        autoOpen: true,
        width: 'auto',
        resizable: false,
        buttons: {
          Yes: function() {
            $(this).remove();
            callback(true);
          },
          No: function() {
            $(this).remove();
            callback(false);
          }
        },
        close: function(event, ui) {
          $(this).remove();
        }
      });
  };


})

答案 1 :(得分:0)

尝试将ajax移动到ConfirmDialog:yes函数中。或者尝试使用诺言。

buttons: {
            Yes: function () {
                 var row = $(this).closest('tr');
                 row.css('opacity','0.3');
                 var word = $(this).closest('td').siblings('.word').text();
                 $.ajax({
            url :  '../../te_addStopWord',
            type : 'GET',
            data: {word : word},
            dataType : 'JSON',
            success : function () {
                row.remove();
            }, // success
            error : function ($a,$b,$c) {
                alert($b);
                row.css('opacity','1');
            }
        }); // ajax
                                $(this).remove();
                                return true;
                            },
            No: function () {
                                $(this).remove();
                                return false;
                            }
                        },
相关问题