Prometheus
Prometheus

Reputation: 33655

Post more JSON on a backbone.js save

I'm using backbone to make a simple POST to my API. However, I need to add additional details to the json post on save about my user. What is the best way and how?

user : {pk:1, name:test}

 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

Upvotes: 0

Views: 98

Answers (1)

homtg
homtg

Reputation: 1999

just add it to your participantDetails variable like this:

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

If you want to add properties to the main object do:

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

Upvotes: 1

Related Questions