简体   繁体   English

Backbone.js-保存对象时不会触发错误事件

[英]Backbone.js - error event not fired when saving an object

I have a Backbone Model with validation logic. 我有一个带有验证逻辑的骨干模型。 I have bound the following method to show the field errors when the validation fails: 我绑定了以下方法,以在验证失败时显示字段错误:

this.model.on('error', this.showError);

When I try to set invalid values to my model, the error event is getting fired properly. 当我尝试为模型设置无效值时, 错误事件被正确触发。

this.model.set(invalidValues); // triggers showError method

But if I try to save the object using the same invalid values, the validation fails as expected, but the error event is not getting fired, hence the showError method is not getting called.. 但是,如果我尝试使用相同的无效值保存对象,则验证会按预期失败,但是不会触发错误事件,因此不会调用showError方法。

this.save(invalidValues, {error:this.failed,success:this.succeeded}); // does not trigger showError

The save does not trigger the error event, but the this.failed is getting called as expected. 保存不会触发错误事件,但是this.failed会按预期被调用。

So, what do I need to do to make sure the that the on error event is always triggered when there's a validation error? 因此,我需要做些什么来确保在发生验证错误时始终触发on error事件?

When you set the error callback in your save options it overrides the default of backbone, which is to trigger the error event. 在保存选项中设置错误回调时,它将覆盖默认的主干,即触发错误事件。 In your error callback, you can trigger the error event on the model or just call your showError method. 在错误回调中,您可以在模型上触发错误事件,也可以只调用showError方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM