在 Backbone.js 中处理模型更新服务器响应
Backbone.js 模型在之前的页面上本地更新自身及其视图 将数据传递到服务器并检查服务器的响应,因此如果 服务器说“对不起查理,这不好”该页面已经显示了数据 已更改,因此不能正确表示对象的服务器端状态。当服务器返回错误时,在 Backbone.js 端处理此问题的正确/优雅的方法是什么?
The Backbone.js model updates itself - and its views - locally on the page before
passing the data to the server and checking the server's response, so if the
server says "sorry charlie that's no good" the page has already shown the data
as having changed and thus doesn't correctly represent the server-side state of the object. What's the correct/elegant way to handle this on the Backbone.js side when the server returns an error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您直接编辑模型并尝试同步它,并且同时在应用程序的其他地方使用它,那么这可能会导致一系列问题。
在许多情况下,最好克隆模型进行编辑,然后当同步成功时,将克隆的属性应用回原始模型。
您可以通过简单地调用来获取克隆模型:
然后您可以将属性应用回成功处理程序中,例如
If you are directly editing a model and trying to sync it, and also using it elsewhere in your app simultaneously, then that can lead to a world of problems.
In many cases it is better to clone the model for editing, and then when it syncs successfully, apply the clone's attributes back on to the original model.
You can get a cloned model by simply calling:
And then you can apply the attributes back in a success handler like
如何将成功、错误回调函数传递给更新服务器端状态的语句。可能是这样的......
更进一步,您可能会覆盖 Backbone.sync 以全局处理服务器端错误代码。
How about passing success, error callback functions to the statement where you update the server side state. May be something like this....
Taking it a level higher, you might override the Backbone.sync to globally handle the server-side error codes.
使用
来源:http://backbonejs.org/#Model-save
Use
Source: http://backbonejs.org/#Model-save