I’ve got an API setup which uses Media Types in the Accept header for all requests. Getting started I just simply wrapped the fetch and save functions of models, but I was curious if there was a better way rather than having to do that in every model and collection?
Update
var accountsCollection = Backbone.Collection.extend({
model: accountModel,
url: '/api/accounts',
vnd: 'application/vnd.app.AccountFeed+json',
return resp.Items;
},
search: function (q) {
this.fetch({
headers: { Accept: this.vnd },
data: {
q: q
},
success: function (data) {
//console.log(q);
}
});
}
});
As in the above example, I’m having to wrap the fetch because of the need to have a custom Accept header. In my opinion a perfect solution would be to be able to extend fetch & save to automatically pull the vnd value of the Collection or Model. I hope this better clarifies what I was asking.
Backbone uses the notion of a sync adapter in order to customize your CRUD operation.
Each collection/model can has its own sync method which could apply your custom http adapter.
Your code could look like: