0

我正在使用主干对我的 API 进行简单的 POST。但是,我需要在保存关于我的用户的 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
4

1 回答 1

1

只需将其添加到您的参与者详细信息变量中,如下所示:

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

如果要向主对象添加属性,请执行以下操作:

var participantDetails = $(ev.currentTarget).serializeObject();
participantDetails.pk = 1;
participantDetails.name = "test";
于 2013-11-10T08:35:14.607 回答