2016-07-04 28 views
1

我有以下編程場景從javascript函數

我想確認標籤的缺失標籤被刪除之前,所以當用戶確認該消息,那麼該標籤將被刪除的問題回調接收值。

我面臨的是回調函數不會返回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

回答

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; 
      },