在主干中,我有一个具有以下结构的模型:
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
}
}]
});
在服务器端我有相同的架构
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],
});
在我的应用程序中,我创建了邀请模型的集合,然后需要将邀请列表发送到服务器以进行保存。稍后我可能会发送一个新集合来更新邀请(删除或添加)。
- 我如何告诉 Backbone 仅将邀请属性发送到服务器(例如 /api/survey/[id]/invites
- 如何告诉服务器清除邀请数组并将其替换为调查输入
- 服务器应该返回什么到 Backbone,以便它在 Backbone Relational 中正确更新(为我的邀请提供正确的 ID)。