In backbone, I have a model with the following structure:
m.Survey = m.BaseModel.extend({
initialize: function() {
this.saveInvites = _.debounce(this.saveInvites, 1000);
},
/**
* What should this function do?!
* @return {[type]}
*/
saveInvites: function(){
},
urlRoot: '/api/surveys',
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
}
}]
});
On the server side I have the same schema
var SurveyInviteSchema = new Schema({
account: {type: Schema.Types.ObjectId, ref: 'Account', required: true},
survey: {type: Schema.Types.ObjectId, ref: 'Survey', required: true},
key: String
});
var SurveySchema = new Schema({
start_date: Date,
end_date: Date,
title: String,
invites: [SurveyInviteSchema],
});
In my application, I create the collection of invite models, and then need to send the list of invites to the server to be saved. Later I may send a new collection to update the invites (removing or adding).
- How do I tell Backbone to send just the invites attribute to the server (to say /api/survey/[id]/invites
- How do I tell the server to clear the invites array and replace it with the survey input
- What should server return to Backbone so that it properly updates in Backbone Relational (giving the correct IDs to my invites).