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

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

问题描述

我不知道人们通常怎么办误差Backbone.js的处理。这将是很好的东西,弹出每次我打电话model.save(这反过来又要求Backbone.sync)。事情是,如何Backbone.js的知道什么时候发生错误或成功在服务器上发生?我明白,如果有一个5​​00服务器错误或类似的东西(这jQuery的知道,因为Backbone.sync调用jQuery.ajax)会知道 - 但我希望能够通过邮件和其他codeS,所以我可以给更有意义的错误信息给用户。

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.

我有一个想法,他会喜欢一些反馈。这个想法是重写Backbone.sync。的新的同步从服务器获取,它必须是在一个特定的格式的响应。这种格式会是这样的:

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

没什么特别,但基本上,而不只是返回平原模式,它包裹了一个响应code 消息其可以显示给用户。

这是正常的方式做到这一点?任何其他办法更好?

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

谢谢!

推荐答案

在我的耳朵,这听起来就复杂了点,至少开始。 Backbone.sync 已经将报告,你可以在你的模型发现错误 .save()方法:

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)...}

文档)。

如果您的服务器端如下 HTTP 规格好了,错误code已经提供( 500 - 服务器错误, 404 - 模型没有找到,你知道..),即使服务器发送一个错误code它仍然可以发送内容(您最佳的消息)。所以,你基本上已经内置在 HTTP 协议本身的所有参数。在我的经验,你少写code,如果你与协议工作在它上面建立新层代替。

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天全站免登陆