You could have a POST route that accepts an array of ids and delete them all at once.
VueJS: Delete multiple tasks. Each task one request?
Please review my code. I am rebuilding Jeffrey's TaskApp and I have a "Clear Completed" Button that does this:
clearCompleted: function () {
var that = this;
var toBeClearedTasks = this.tasks.filter(function (task) {
return task.completed;
});
toBeClearedTasks.forEach(function (task) {
that.$http.delete('/api/tasks/' + task.id, task);
});
this.tasks = this.tasks.filter(function (task) {
return !task.completed;
});
}
As you can see, I have a forEach loop, that loops through each completed task, that should be deleted through a http request.
Works fine for a small number of tasks. But when I Complete All tasks and then immediately Clear All Completed and then refresh the page. The tasks are still there. If I wait a couple of seconds for the requests to be complete, it works fine again.
Is there a way to use something like a queue? Or is there a way to send a single request and then use the foreach loop on the server? How were you going to solve a problem, where you maybe have hundreds of tasks that you want to delete. Firing for each and single one a delete request?
EDIT: Also how would you refactor the code?
Please or to participate in this conversation.