Backbone.js:如何从 collection.create 获取模型验证错误

Backbone.js: How to get model validation error from collection.create

正在尝试使用 collection.create 添加模型。我在模型上添加了验证,它的工作原理是当我尝试创建模型时没有任何反应。然而,成功或错误回调都没有被触发,页面就在那里。我如何找回实际错误? (验证只是 returns 一个像 "this is wrong" 这样的字符串)

我认为问题在于错误回调仅在服务器返回错误时触发。不过,您如何从这里捕获验证错误?

self.collection.create({
     //attributes
   }, {
   success: function (model, response) {
       //this doesn't run
   },
   error: function (model, response) {
       //this doesn't run either
   }
});

来自文档

Returns the new model. If client-side validation failed, the model will be unsaved, with validation errors

var model = self.collection.create({
     //attributes
   }, {
   success: function (model, response) {
       //this doesn't run
   },
   error: function (model, response) {
       //this doesn't run either
   }
});

现在您可以通过两种方式处理错误:
1) using events

model.on('invalid',function(){
  // error handling here
});


2) using flag

   if(model.validationError){
       // error handling here
        }