使用Backbone Relational + Mongoose,如何RESTful保存属于模型属性的集合?

时间:2013-02-01 21:31:06

标签: backbone.js mongoose backbone-relational

在骨干网中,我有一个具有以下结构的模型:

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],
});

在我的应用程序中,我创建了邀请模型的集合,然后需要将邀请列表发送到要保存的服务器。稍后我可能会发送一个新的集合来更新邀请(删除或添加)。

  1. 如何告诉Backbone仅将邀请属性发送到服务器(例如/ api / survey / [id] / invites
  2. 如何告诉服务器清除邀请数组并将其替换为调查输入
  3. 服务器应该返回Backbone以便在Backbone Relational中正确更新(为我的邀请提供正确的ID)。

0 个答案:

没有答案