Reputation: 5777
I have a collection that's attached to a model. When I click a button, I would like to be able to tell backbone to save only that one attribute (containing the collection) to the server
m.Survey = m.BaseModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'invites',
relatedModel: 'APP.Models.SurveyInvite',
collectionType: 'APP.Collections.SurveyInvites',
//save invites separately
includeInJSON: false,
reverseRelation: {
key: 'survey',
//We don't want to list the survey when doing toJSON()
includeInJSON: false
}
}],
//need this method
saveInvites: function(){
this.saveOnly('invites');
});
});
And I want it to send to the server:
POST /api/surveys/123/
{
invites: [
{<invite1>}, {<invite2>}, {<invitex>}
]
}
Upvotes: 0
Views: 169
Reputation: 35920
You can use Model.save
with the patch
option:
saveInvites: function(){
this.save({invites:this.get('invites')}, {patch:true});
});
Instead of a POST
request, this will send a HTTP PATCH
. Since you were asking for a RESTful way, patch is the correct verb to use here. If your server can't handle the patch request, you can force it to POST
with the emulateHTTP
option:
saveInvites: function(){
this.save({invites:this.get('invites')}, {patch:true, emulateHTTP:true});
});
Upvotes: 3