0

モデルに添付されたコレクションがあります。ボタンをクリックしたときに、その 1 つの属性 (コレクションを含む) のみをサーバーに保存するようにバックボーンに指示できるようにしたいと考えています。

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');
    });
});

そして、サーバーに送信したい:

POST /api/surveys/123/

{
    invites: [
    {<invite1>}, {<invite2>}, {<invitex>}
    ] 
}
4

1 に答える 1

3

Model.saveオプションで使用できpatchます:

saveInvites: function(){
     this.save({invites:this.get('invites')}, {patch:true});
});

POSTリクエストの代わりに、これはHTTP PATCH. あなたは RESTful な方法を求めていたので、パッチはここで使用する正しい動詞です。サーバーがパッチ リクエストを処理できない場合は、次POSTemulateHTTPオプションを使用して強制的に処理できます。

saveInvites: function(){
     this.save({invites:this.get('invites')}, {patch:true, emulateHTTP:true});
});
于 2013-02-07T22:12:22.623 に答える