这是我填充模型属性的方式:
this.model.set('questionAnswers', arrQuestions);
现在提交时,我检查模型是否有效:
if (this.model.isValid()) {
this.model.save(null, { success: this.gradingQuestionsSuccess, error: this.gradingQuestionsFailed });
}
验证的工作原理如下:
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;
}
服务调用是:
url: function () {
var url = Application.getConfig("url") + "/";
url += Application.getConfig("v2path3") + "/account/submitGradingQuestions";
}
return url;
}
模型有效,并且在提交时设置了值,但它没有在请求负载中发送它。
谁能帮我理解为什么会发生这种情况?提前致谢!
最佳答案
Backbone 不会观察更改的属性并自动同步save(null)
。您需要的是将属性传递给 Model.save
方法,该方法在幕后进行 set
here或here取决于 wait
选项的属性。所以你只需要以下内容:
var data = {questionAnswers: arrQuestions};
this.model.save(data, {
success: this.gradingQuestionsSuccess,
error: this.gradingQuestionsFailed
});
您也不需要手动调用验证,因为它是在 save method 中调用的。还有。
关于javascript - 主干模型未在 SAVE 上发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32049842/