2015-04-02 53 views
0

尝试使用collection.create添加模型。我在模型上添加了验证,并且在我尝试创建模型时没有任何反应。但是,无论是成功还是错误回调都不会触发,页面就会在那里。我如何得到实际的错误? (验证只是返回一个像“这是错误的”字符串)Backbone.js:如何从collection.create获取模型验证错误

我相信问题是错误回调仅在服务器返回的错误上触发。但是,如何从这里捕获验证错误?

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

回答

2

从文档

返回新模式。如果客户端验证失败,该模型将未保存,验证错误

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

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

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


2)using flag

if(model.validationError){ 
     // error handling here 
     } 
相关问题