【问题标题】:Backbone model not sending data on SAVE骨干模型未在 SAVE 上发送数据
【发布时间】:2015-08-17 12:00:43
【问题描述】:

这是我填充模型属性的方式:

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;          
        }

模型是有效的,并且在提交时在其中设置了值,但它没有在请求有效负载中发送它。

谁能帮我理解为什么会这样?提前致谢!

【问题讨论】:

    标签: javascript backbone.js marionette backbone-model


    【解决方案1】:

    Backbone 不会观察更改的属性并自动在save(null) 上同步。您需要将属性传递给 Model.save 方法,根据 wait 选项的属性,在 herehere 下生成 set。所以你只需要以下内容:

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

    您也不需要手动调用验证,因为它也在 save method 中被调用。

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 2013-11-08
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 2013-10-14
      • 2011-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多