2013-02-07 3 views
0

모델에 첨부 된 컬렉션이 있습니다.하나의 속성 만 서버에 저장하도록 백본에게 알리는 편리한 방법이 있습니까?

: 나는 버튼을 클릭하면, 나는 서버

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/조사/123/

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

답변

3

을 당신은 patch 옵션 Model.save을 사용할 수 있습니다

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

POST 요청 대신 HTTP PATCH이 전송됩니다. RESTful 방법을 요구 했으므로 패치는 여기서 사용하는 올바른 동사입니다. 서버가 패치 요청을 처리 할 수없는 경우, 당신은 emulateHTTP 옵션으로 POST에 강제 할 수

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

감사합니다! 나는 그것이 바로 그런 것이 었음을 알았고, 올바른 동사를 알지 못했다.) –

관련 문제