Reputation: 37
I use fetch to retrieve my models from the server. It works great when I have data in my DB but how to handle an empty response send by the server to the client ?
For example if no data have been already saved by the user the server send a http 200 response with an empty array and backbone triggers an error callback but I just want to inform the user that there is no data saved in DB. In this case an empty response just means there is no model to load and I don't want to create any model with the response.
Here is the code I use :
app.plans.fetch({
success: function(data) {
app.Notifications.updateMessages({text: "Plans loaded."});
},
error: function (){
app.Notifications.updateMessages({text: "Error."});
}
});
How the server can indicate that it's all right but there is just no data ?
Upvotes: 1
Views: 735
Reputation: 313
If you are responsible for back-end and you can change response behavior you should use http status 204 for empty content response instead of 200
this should help
Upvotes: 2
Reputation: 36
If server responds with the 200 it shouldn't trigger an error callback. It's weird. Could you please add the example of the server response?
Also success and error callbacks take the following options as arguments: (collection, response, options)
Upvotes: 0