Skip to content

Instantly share code, notes, and snippets.

@wdog
Created September 6, 2016 15:42
Show Gist options
  • Save wdog/885ee8473cf96cd1bd7fbd336e6fec45 to your computer and use it in GitHub Desktop.
Save wdog/885ee8473cf96cd1bd7fbd336e6fec45 to your computer and use it in GitHub Desktop.
<script language="javascript">
var deleter = {
linkSelector: "a[data-delete]",
modalTitle: "Confermi Eliminazione?",
modalMessage: "Questo dato non potra' essere recuperato?",
modalCancelButtonText:'Annulla',
modalConfirmButtonText: "Si Elimina!",
laravelToken: null,
url: "/",
init: function () {
$(this.linkSelector).on('click', {self: this}, this.handleClick);
},
handleClick: function (event) {
event.preventDefault();
var self = event.data.self;
var link = $(this);
self.modalTitle = link.data('title') || self.modalTitle;
self.modalMessage = link.data('message') || self.modalMessage;
self.modalConfirmButtonText = link.data('button-text') || self.modalConfirmButtonText;
self.url = link.attr('href');
self.laravelToken = $("meta[name=token]").attr('content');
self.confirmDelete();
},
confirmDelete: function () {
swal({
title: this.modalTitle,
text: this.modalMessage,
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: this.modalConfirmButtonText,
cancelButtonText: this.modalCancelButtonText,
closeOnConfirm: true
},
function () {
this.makeDeleteRequest()
}.bind(this)
);
},
makeDeleteRequest: function () {
var form =
$('<form>', {
'method': 'POST',
'action': this.url
});
var token =
$('<input>', {
'type': 'hidden',
'name': '_token',
'value': "{!! csrf_token(); !!}"
});
var hiddenInput =
$('<input>', {
'name': '_method',
'type': 'hidden',
'value': 'DELETE'
});
return form.append(token, hiddenInput).appendTo('body').submit();
}
};
deleter.init();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment