Abhishek
Abhishek

Reputation: 2034

Backbone model not sending data on SAVE

This is the way I'm populating the attribute of my model:

this.model.set('questionAnswers', arrQuestions);

Now on submit, I check if model is valid:

 if (this.model.isValid()) {
                this.model.save(null, { success: this.gradingQuestionsSuccess, error: this.gradingQuestionsFailed });
            }

Validations works like this:

validate: function (attr, options) {
            var error = null;

            if (attr.questionAnswers.length < this.cntQues) {
                this.trigger('empty:answers');
                error = 'Please answer all the questions.';
            }
            return error;
        }

And service call is:

 url: function () {
            var url = Application.getConfig("url") + "/";
            url += Application.getConfig("v2path3") + "/account/submitGradingQuestions";
            } 
            return url;          
        }

The model is valid and the values are set in it on Submit, but it's not sending it in the Request Payload.

enter image description here

Can anyone please help me understand why this is happening? Thanks in advance!

Upvotes: 0

Views: 571

Answers (1)

Artem Baranovskii
Artem Baranovskii

Reputation: 1003

Backbone does not observe the changed attributes and sync on save(null) automatically. What you need is pass attributes to Model.save method that makes set under the hood here or here depending on wait option's property. So you just need the following:

var data = {questionAnswers: arrQuestions};

this.model.save(data, {
   success: this.gradingQuestionsSuccess,
   error: this.gradingQuestionsFailed 
});

You also don't need invoke validation manually because it's get invoked in save method also.

Upvotes: 1

Related Questions