2013-11-09 45 views
0

我正在使用骨幹對我的API進行簡單的POST。但是,我需要在json文章中添加額外的細節以保存我的用戶。什麼是最好的方式和方式?在backbone.js上發佈更多JSON保存

用戶:{PK:1,名稱:測試}

var ParticipantView = Backbone.View.extend({ 
     el: '#participant-panel', 
     events: { 
      'submit #participant-form': 'saveParticipant' 
     },// end of events 
     saveParticipant: function (ev) { 
      var participantDetails = $(ev.currentTarget).serializeObject(); 
      var participant = new Participant(); 

      participant.save(participantDetails, { 
      success: function (participant) { 
       alert("created") 
       }, 
      error: function (model, response) { 
       console.log('error', model, response); 
      } 
      });// end of participant save function 

      return false; // make sure form does not submit 
     }// end of save participants 
    });// end of participant view 

回答

1

只需將其添加到您的participantDetails變量是這樣的:

var participantDetails = $(ev.currentTarget).serializeObject(); 
var userinfo = {pk: 1, name: "test"}; 
participantDetails.user = userinfo; 

如果你想屬性添加到主object do:

var participantDetails = $(ev.currentTarget).serializeObject(); 
participantDetails.pk = 1; 
participantDetails.name = "test";