jquery - Receive value from callback javascript function -
i having problem following programming scenario
i want confirm deletion of tag before tag deleted when user confirm message tag removed.
what facing callback function doesn't return true value , returning null due asynchronous js callback.
here code
$(document).ready(function () { $("#categories").tagit({ allowspaces: true, beforetagremoved: function (evt, ui) { var isdeleted; $.confirm({ title:"deactivate confirmation", text:"are sure want deactivate idea? users not able see idea more.", confirm: function(button) { isdeleted = true; }, cancel: function(button) { isdeleted = false; }, confirmbutton: "yes", cancelbutton: "no", confirmbuttonclass: "btn-danger", }); return isdeleted; // here problem, returns null due 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>'); } } }); } } }); });
any idea how solve issue , deletion after message confirmed ?
libraries used: tagit.js, jquery.confirm.js
i return false
beforetagremoved callback , call $.confirm inside it. , in confirm
callback of $.confirm manually trigger tag removing
upd: var isconfirmed = false;
beforetagremoved: function (evt, ui) { $.confirm({ title:"deactivate confirmation", text:"are sure want deactivate idea? users not able see idea more.", confirm: function(button) { isconfirmed = true; // trigger removing isconfirmed set true $("#mytags").tagit("removetagbylabel", "my-tag"); }, cancel: function(button) { isconfirmed = false; }, confirmbutton: "yes", cancelbutton: "no", confirmbuttonclass: "btn-danger", }); return isconfirmed; },
Comments
Post a Comment