从回调javascript函数接收值

时间:2016-07-04 12:50:57

标签: javascript jquery jquery-plugins

我遇到以下编程方案的问题

我想在删除标签之前确认删除标签,因此当用户确认消息时,标签将被删除。

我面临的是回调函数没有返回true值,并且由于异步js回调而返回null。

这是代码

$(document).ready(function () {
        $("#categories").tagit({
            allowSpaces: true,
            beforeTagRemoved: function (evt, ui) {
                var isDeleted;
                $.confirm({
                    title:"Deactivate confirmation",
                    text:"Are you sure you want to deactivate this idea? Users will not be able to see this idea any more.",
                    confirm: function(button) {
                        isDeleted = true;
                    },
                    cancel: function(button) {
                        isDeleted = false;
                    },
                    confirmButton: "Yes",
                    cancelButton: "No",
                    confirmButtonClass: "btn-danger",
                });
                return isDeleted; // here is the problem, it returns null due to callback $.confirm function
            },
            afterTagRemoved: function (evt, ui) {
                $.ajax({
                    url: "/admin/categories/",
                    type: "POST",
                    data: '_method=delete&' + 'category=' + $("#categories").tagit('tagLabel', ui.tag) + '&_token={{csrf_token()}}',
                    success: function (data) {
                        if (data['status'] == 'success') {
                            $(".box-footer").html(
                                    '<div class="alert alert-success alert-dismissible">' +
                                    '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' +
                                    data['message'] +
                                    '</div>');
                        }
                        else {
                            $(".box-footer").html(
                                    '<div class="alert alert-danger alert-dismissible">' +
                                    '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' +
                                    data['message'] +
                                    '</div>');
                        }
                    }
                });
            },
            afterTagAdded: function (evt, ui) {
                if (!ui.duringInitialization) {
                    $.ajax({
                        url: "/admin/categories/",
                        type: "POST",
                        data: '_method=post&' + 'category=' + $("#categories").tagit('tagLabel', ui.tag) + '&_token={{csrf_token()}}',
                        success: function (data) {
                            console.log(data);
                            if (data['status'] == 'success') {
                                $(".box-footer").html(
                                        '<div class="alert alert-success alert-dismissible">' +
                                        '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' +
                                        data['message'] +
                                        '</div>');
                            }
                            else {
                                $(".box-footer").html(
                                        '<div class="alert alert-danger alert-dismissible">' +
                                        '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' +
                                        data['message'] +
                                        '</div>');
                            }
                        }
                    });
                }
            }
        });
    });

知道如何解决问题并在确认消息后删除?

使用的库:Tagit.js,jquery.confirm.js

1 个答案:

答案 0 :(得分:0)

我总是从beforeTagRemoved回调中返回false并在其中调用$ .confirm。在$ .confirm的confirm回调中,我会手动触发标记删除

UPD: var isConfirmed = false;

beforeTagRemoved: function (evt, ui) {

                $.confirm({
                    title:"Deactivate confirmation",
                    text:"Are you sure you want to deactivate this idea? Users will not be able to see this idea any more.",
                    confirm: function(button) {
                        isConfirmed = true;
                        // trigger removing with isConfirmed set to true
                        $("#myTags").tagit("removeTagByLabel", "my-tag");
                    },
                    cancel: function(button) {
                        isConfirmed = false;
                    },
                    confirmButton: "Yes",
                    cancelButton: "No",
                    confirmButtonClass: "btn-danger",
                });
                return isConfirmed; 
            },