0

这是我在添加sweetalert2删除帖子之前的代码:

if (action == "delete") {
            this.model.destroy({
                beforeSend: function() {
                    target.addClass('loading');
                    view.blockUi.block(view.$el);
                },
                success: function(result, status, jqXHR) {
                    view.blockUi.unblock();
                    target.removeClass('loading');
                    if (status.success) {

                        if (result.get('post_type') == "post")
                            window.location.href = status.redirect;
                        else
                            view.$el.fadeOut();
                    } else {
                        // Error
                    }
                }
            });
            return false;
        }

这是我的编辑,使 sweetalert2 与操作兼容:

if (action == "delete") {

            swal({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                type: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then(function () {
                swal(
                    'Deleted!',
                    'Your post has been deleted.',
                    'success'
                ),
                  this.model.destroy({
                    beforeSend: function() {
                        target.addClass('loading');
                        view.blockUi.block(view.$el);

                    },

                        success: function(result, status, jqXHR) {
                            view.blockUi.unblock();
                            target.removeClass('loading');
                            if (status.success) {

                                if (result.get('post_type') == "post")
                                    window.location.href = status.redirect;
                                else
                                    view.$el.fadeOut();
                            } else {
                                // Error
                            }

                    }
                })
            });
            return false;
        }

我找不到 sweetalert2 对话框正常工作但删除帖子的操作不起作用的错误,我该怎么办?

4

1 回答 1

0

我找不到 sweetalert2 对话框正常工作但删除帖子的操作不起作用的错误,我该怎么办?

当您最初调用 sweetalert 时,它会提示用户做出响应。

then() 方法返回一个 Promise。它最多需要两个参数:Promise 成功和失败情况的回调函数。

如果用户确认,则可以执行代码。您已经实现了一种捕获成功和错误的方法,因此当其中任何一种发生时,您只需再次调用 sweetalert 即可覆盖之前的方法并向用户显示正确的警报。如果用户决定取消以向他们提供更多反馈,您也可以选择这样做。

我相信这可以解决问题:

if (action == "delete") {

swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
}).then(function () {

      this.model.destroy({
        beforeSend: function() {
            target.addClass('loading');
            view.blockUi.block(view.$el);

        },
            success: function(result, status, jqXHR) {
                view.blockUi.unblock();
                target.removeClass('loading');
                if (status.success) {
                    // Success
                    swal(
                        'Deleted!',
                        'Your file has been deleted.',
                        'success'
                      )
                } else {
                    // Error
                    swal(
                      'Failed',
                      'Your file has not been deleted',
                      'error'
                    )
                }

        }
    })
}, function () {
// Cancelled
    swal(
      'Cancelled',
      'Your file has not been deleted',
      'error'
    )
});

return false;
}
于 2017-01-13T00:00:01.993 回答