The documentation for deleteItem
says:
If deletion is asynchronous, method should return jQuery promise that will be resolved when deletion is completed.
What does this really mean? Must the promise return any data?
Will the following work?
deleteItem: function(item) {
return $.ajax("/api/members/", {
method: "DELETE",
data: { item: item},
error: (err) => { console.log(err);},
success: (result, status, jqXHR) => {},
})
}
The above works for me in testing but I am not sure whether it is right to just ignore if the Ajax call is successful.
If your business logic tolerates the failure of deleting the item then it is fine not to handle it.
In most cases this is not the case, you should handle the failure of deleting the item and tell the user properly about this failure, may be with an elegant message to ask him to try later.
About "what must deleteItem return?" :
It should return a status code of 200 on success with empty body. And it should return an appropriate http status code with error body that suits the reason of failing. e.g: if the item you are trying to delete doesn't exist, then error of status code 422 (Unprocessable Entity) is expected to be returned.
e.g if you are not authorized to delete this item, an error of 403 status code shall be returned.
In summary, yes you should handle the error and tell the user about it.
Your code should somehow look like:
deleteItem: function(item) {
return $.ajax("/api/members/", {
method: "DELETE",
data: { item: item},
error: (jqXHR , err) => {
if(jqXHR.status == 403){
console.log("handle forbidden error code);
alert("You are not authorized to delete this item, check
with your manager...");
}
},
success: (result, status, jqXHR) => {},
})
}