Backbone.js 错误处理 - 你是怎么做的? [英] Backbone.js Error Handling - how do you do it?

查看:15
本文介绍了Backbone.js 错误处理 - 你是怎么做的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道人们通常如何使用backbone.js 进行错误处理.每次我调用model.save(它又调用Backbone.sync)时弹出一些东西会很好.问题是,backbone.js 如何知道服务器何时发生错误或成功?我知道它会知道是否有 500 服务器错误或类似的错误(自从 Backbone.sync 调用 jQuery.ajax 以来,jquery 就知道了) - 但我希望能够传递消息和其他代码,以便我可以给出更有意义的错误给用户的消息.

我有一个想法,希望得到一些反馈.这个想法是覆盖 Backbone.sync.新同步从服务器获得响应,响应必须采用特定格式.这种格式类似于:

服务器响应对象:>响应码>信息>模型

没什么特别的,但基本上,它不是简单地返回普通模型,而是用一个 ResponseCodeMessage 包裹起来,可以向用户显示.>

这是正常的做法吗?还有其他更好的方法吗?

谢谢!

解决方案

在我看来,这听起来有点复杂,至少一开始是这样.Backbone.sync 将报告您可以在模型中捕获的错误 .save() 方法:

this.mymodel.save(/* ... */, {success: function(model, result, xhr)...,错误:函数(模型,xhr,选项)...}

(文档).

如果您的服务器端遵循 HTTP 规范,那么错误代码是已经提供(500 - 服务器错误,404 - 找不到模型,你知道的..),即使服务器发送错误代码它仍然可以发送内容(完美为您的消息).因此,您基本上已经将所有参数内置到 HTTP 协议本身中.根据我的经验,如果您使用协议而不是在其上构建新层,那么您可以编写更少的代码.

在上面的 error 回调中,您可能很有可能调用系统的其余部分并将错误发布到某个应用程序消息总线或类似的(通过 Backbones 自己的事件机制或某些 专用 ).

I'm wondering how people typically do error handling with backbone.js. It would be nice for something to popup everytime I call model.save (which in turn calls Backbone.sync). The thing is, how does backbone.js know when an error or a success has occurred on the server? I understand it would know if there was a 500 server error or something like that (which jquery knows about since Backbone.sync calls jQuery.ajax) - but I want to be able to pass messages and other codes so I can give more meaningful error messages to the user.

I have one idea and would love some feedback. The idea is to override Backbone.sync. The new sync gets a response from the server, which must be in a particular format. This format would be something like:

ServerResponseObject:
  > ResponseCode
  > Message
  > Model

Nothing fancy, but basically, instead of just returning the plain model, it is wrapped up with a ResponseCode and Message which can be shown to the user.

Is this the normal way to do it? Any other approach that is better?

Thanks!

解决方案

In my ears this sounds a bit on the complex side, at least to start with. Backbone.sync will already report errors that you can catch in your models .save() method:

this.mymodel.save(/* ... */, {success: function(model, result, xhr)...,
                              error: function(model, xhr, options)...}

(docs).

If your serverside follows HTTP specs well, the error code is already provided (500 - server error, 404 - model not found, you know..), and even if the server sends an error code it can still send content (perfect for your message). So you basically already have all parameters built in to the HTTP protocol itself. In my experience you get to write less code if you work with the protocol instead of building new layers on top of it.

In your errorcallback above, you probably have good possibilities to call the rest of your system and post an error to some application message bus or similar (via Backbones own event mechanism or some dedicated library).

这篇关于Backbone.js 错误处理 - 你是怎么做的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆