In my Backbone.js based app I am talking to my API that responds with a 204 status and an empty body, in case a collection is requested which does not contain any data yet. That’s in my opinion how a RESTful API should respond in such case.
In my app now I have the problem, that obviously no event is triggered after a 204 response was received. I tried to bind reset and all like:
FoosCollectionView.prototype.initialize = function() {
this.collection = new FoosCollection;
this.collection.bind('reset', this.render, this);
this.collection.bind('all', this.render, this);
return this.collection.fetch();
};
but the events never fire. So I tried to give fetch some callbacks:
FoosCollectionView.prototype.initialize = function() {
this.collection = new FoosCollection();
return this.collection.fetch({
success: function(a, b, c) {
debugger;
},
error: function(a, b, c) {
debugger;
},
complete: function(a, b) {
debugger;
}
});
};
Same behaviour. No debug statement is ever reached in case the response is a 204. How can I handle 204 responses then? Will I have to dig down into sync and add an extra handling for 204 there or is there something in Backbone that I simply don’t know yet?
Thx Felix
The solution seems pretty forward and awkward at the same time:
I simply define the
parsemethod in my collections so that it checks, whether the passed response object is empty. That’s only the case when a 204 occured. Then insideparseI setthis.collection.models = []which triggers aresetevent. The collections view is bound to that event, runs a function which can have a look insidethis.collection.models. In case no models are given, a ‘no-content’ template can be rendered instead the standard template.If someone has a better approach, I’d appreciate to here that!