Backbone.js服务器端验证和其他服务器端错误

时间:2022-05-26 16:51:53

How do you roll back model changes when encountering server-side errors (e.g. validation errors)?

当遇到服务器端错误(例如验证错误)时,如何回滚模型更改?

Given that certain validation must be done on the server side, what is the appropriate way to do this with backbone.js (Rails backend)?

鉴于必须在服务器端进行某些验证,使用backbone.js(Rails后端)执行此操作的适当方法是什么?

When saving a backbone model, client-side validation fires which gives the appropriate user experience if validation fails (views of that model don't update). However, if server-side validation fails, the model and all of its views have already been updated (with invalid data) before the PUT to the server.

保存骨干模型时,如果验证失败,则会触发客户端验证,从而提供适当的用户体验(该模型的视图不会更新)。但是,如果服务器端验证失败,则在PUT到服务器之前,模型及其所有视图已经更新(使用无效数据)。

There seem to be a few problems with this.

这似乎有一些问题。

  1. All views are updated before the model has been server-side validated. If, for instance, you have a list of models with a popup edit dialog, the model in the list is updated with potentially non-validatable info after you call Model.save but before it has been server-side validated and PUT'ed.
  2. 在模型经过服务器端验证之前,所有视图都会更新。例如,如果您有一个带有弹出编辑对话框的模型列表,则在调用Model.save之后但在进行服务器端验证和PUT之前,列表中的模型将使用可能不可验证的信息进行更新。
  3. If the server returns an error (e.g. 422 error), no 'rollback' of the model occurs. The unvalidatable data is just sitting there like a turd. This is really the bad one.
  4. 如果服务器返回错误(例如422错误),则不会发生模型的“回滚”。不可验证的数据就像粪便一样坐在那里。这真的很糟糕。

Am I using backbone.js wrong? Is there a well-known way to handle this (very common) scenario? I understand I can do some manual caching of the old values, etc, but it's kind of a smelly solution.

我使用backbone.js错了吗?是否有一种众所周知的方法来处理这种(非常常见的)情景?我知道我可以对旧值进行一些手动缓存等,但这是一种有臭味的解决方案。

Thanks!

谢谢!

2 个解决方案

#1


5  

Don't know if I am doing this wrong (new to BackboneJS), but I had the same problem and here's how I solved it:

不知道我是否做错了(BackboneJS新手),但我遇到了同样的问题,这就是我解决它的方法:

  • I do all my validations server-side

    我在服务器端进行所有验证

  • Instead of doing a normal model.save, I make a standard ajax call to the server and return an error message or a success message containing the attributes of the modified model. If it is a success, I can then do model.set with the returned attributes in order to update the model and the corresponding view.

    我没有做正常的model.save,而是对服务器进行标准的ajax调用,并返回包含修改模型属性的错误消息或成功消息。如果成功,我可以使用返回的属性执行model.set以更新模型和相应的视图。

  • If you want to do client-side validation first, I guess you could do a save with the { silent: true } option, so that views are not updated, then do the ajax call, and see what needs to be done according to the response (restore original values for the model if error or update views if success)

    如果你想先做客户端验证,我想你可以使用{silent:true}选项进行保存,这样就不会更新视图,然后执行ajax调用,看看需要根据需要做什么响应(如果错误则恢复模型的原始值,如果成功则恢复更新视图)

Hope this helps.

希望这可以帮助。

ps: this works, but does not feel "clean". If there's a better solution, I would also love to read it

ps:这有效,但感觉不到“干净”。如果有更好的解决方案,我也很乐意阅读它

#2


0  

What I would do is on the server side make sure you catch any errors and before returning the response query for the original record from the DB and return that as JSON along with the error response. Then you could just do this:

我要做的是在服务器端确保捕获任何错误,然后从数据库返回原始记录的响应查询,并将其作为JSON和错误响应返回。然后你可以这样做:

model.save({}, {
  error: function(model, response){
    model.set(response);
  }
});

Assuming your views are then watching for change events on the model, they will update accordingly.

假设您的视图正在观察模型上的更改事件,它们将相应地更新。

#1


5  

Don't know if I am doing this wrong (new to BackboneJS), but I had the same problem and here's how I solved it:

不知道我是否做错了(BackboneJS新手),但我遇到了同样的问题,这就是我解决它的方法:

  • I do all my validations server-side

    我在服务器端进行所有验证

  • Instead of doing a normal model.save, I make a standard ajax call to the server and return an error message or a success message containing the attributes of the modified model. If it is a success, I can then do model.set with the returned attributes in order to update the model and the corresponding view.

    我没有做正常的model.save,而是对服务器进行标准的ajax调用,并返回包含修改模型属性的错误消息或成功消息。如果成功,我可以使用返回的属性执行model.set以更新模型和相应的视图。

  • If you want to do client-side validation first, I guess you could do a save with the { silent: true } option, so that views are not updated, then do the ajax call, and see what needs to be done according to the response (restore original values for the model if error or update views if success)

    如果你想先做客户端验证,我想你可以使用{silent:true}选项进行保存,这样就不会更新视图,然后执行ajax调用,看看需要根据需要做什么响应(如果错误则恢复模型的原始值,如果成功则恢复更新视图)

Hope this helps.

希望这可以帮助。

ps: this works, but does not feel "clean". If there's a better solution, I would also love to read it

ps:这有效,但感觉不到“干净”。如果有更好的解决方案,我也很乐意阅读它

#2


0  

What I would do is on the server side make sure you catch any errors and before returning the response query for the original record from the DB and return that as JSON along with the error response. Then you could just do this:

我要做的是在服务器端确保捕获任何错误,然后从数据库返回原始记录的响应查询,并将其作为JSON和错误响应返回。然后你可以这样做:

model.save({}, {
  error: function(model, response){
    model.set(response);
  }
});

Assuming your views are then watching for change events on the model, they will update accordingly.

假设您的视图正在观察模型上的更改事件,它们将相应地更新。