Is there a way to optimize the delete function script ? #242
-
For each row the delete javascript script returned in ajax is around 3kb (3000 bytes) Since they are all identical for each row can the script be moved somewhere else? I tried to put it in datatables_logic.blade.php and it works fine but there is probably a better place. @if ($crud->hasAccess('delete'))
<script>
if (typeof deleteEntry != 'function') {
$("[data-button-type=delete]").unbind('click');
function deleteEntry(button) {
// ask for confirmation before deleting an item
// e.preventDefault();
var button = $(button);
var route = button.attr('data-route');
var row = $("#crudTable a[data-route='"+route+"']").closest('tr');
swal({
title: "{!! trans('backpack::base.warning') !!}",
text: "{!! trans('backpack::crud.delete_confirm') !!}",
icon: "warning",
buttons: {
cancel: {
text: "{!! trans('backpack::crud.cancel') !!}",
value: null,
visible: true,
className: "bg-secondary",
closeModal: true,
},
delete: {
text: "{!! trans('backpack::crud.delete') !!}",
value: true,
visible: true,
className: "bg-danger",
}
},
}).then((value) => {
if (value) {
$.ajax({
url: route,
type: 'DELETE',
success: function(result) {
if (result == 1) {
// Show a success notification bubble
new Noty({
type: "success",
text: "{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}"
}).show();
// Hide the modal, if any
$('.modal').modal('hide');
// Remove the details row, if it is open
if (row.hasClass("shown")) {
row.next().remove();
}
// Remove the row from the datatable
row.remove();
} else {
// if the result is an array, it means
// we have notification bubbles to show
if (result instanceof Object) {
// trigger one or more bubble notifications
Object.entries(result).forEach(function(entry, index) {
var type = entry[0];
entry[1].forEach(function(message, i) {
new Noty({
type: type,
text: message
}).show();
});
});
} else {// Show an error alert
swal({
title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}",
text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}",
icon: "error",
timer: 4000,
buttons: false,
});
}
}
},
error: function(result) {
// Show an alert with the result
swal({
title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}",
text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}",
icon: "error",
timer: 4000,
buttons: false,
});
}
});
}
});
}
}
// make it so that the function above is run after each DataTable draw event
// crud.addFunctionToDataTablesDrawEventQueue('deleteEntry');
</script>
@endif
` |
Beta Was this translation helpful? Give feedback.
Replies: 0 comments 5 replies
-
Does anyone have any feedback? 😟 |
Beta Was this translation helpful? Give feedback.
-
@jrbecart I think the solution is to do what we do in fields, use the Sorry I haven't had time to focus on this one myself as we are pushing the new 5.1 with some goodies and bug fixes, we can then look back at optimizations. If you want to contribute and need my help on something please feel free to submit the PR and I give my inputs the best I can. Cheers |
Beta Was this translation helpful? Give feedback.
-
I confirm it's not optimized yet. If you remove the script from the button blade template For 10 rows it's 27 kb less to transfer (10-3kb) |
Beta Was this translation helpful? Give feedback.
Hello @jrbecart thanks for the suggestion. Is something that already bothered me in the past, but I was working on something else and didn't prioritize it.
I decided to get a quick win today, I've just submitted a PR that should address this. Laravel-Backpack/CRUD#4834
I've used the
@loadOnce
directive.You can test that branch by doing:
composer require backpack/crud:"dev-optimize-delete-script as 5.4.99"
Don't forget that if you are overriding the delete button on your
app/resources
folder you will not get the updated version and need to manually add the changes.Wish you the best 🎅