错误回调总是被触发,即使它成功了
我的语法中应该有一个小故障,或者我不理解的东西,但是当我在我的模型上执行保存方法时。无论该方法的结果如何,都会调用错误回调。
@new_activity = new Activity()
@new_activity.save
know: $('input#know').val()
learn: $('input#learn').val()
success: -> console.log 'success'
error: -> console.log 'error'
就我而言,由于我并不真正知道 new_activity 是否已有效通过验证,因此我必须使用一个丑陋的技巧来将活动添加到集合中。 (顺便说一句,我不使用创建方法,因为我确实希望有不同的错误,而不是简单的“错误”。
if @new_activity.has('know') and @new_activity.has('learn')
app.collections.activities.add @new_activity
但是当它成功时;会出现创建模型的警报。
编辑: 更多细节。 这是我的模型: 初始化:-> _.bindAll @, '验证', 'errorHandler' @.bind 'error', @errorHandler
validate: (attrs) ->
errors = []
# We only support a certain number of languages, we enforce that the user does not select a wrong set.
if _.isEmpty(_.intersection([attrs.know], ['en', 'fr'])) is true
errors.push 'This language is not currently supported.'
if _.isEmpty(_.intersection([attrs.learn], ['en', 'fr', 'de', 'es', 'zh', 'pt', 'ar', 'ja', 'ru'])) is true
errors.push 'You cannot learn this language yet.'
if _.isEmpty(errors) is false
errors
errorHandler: (model, error) ->
console.log error
当验证发生时,如果validate方法没有返回任何内容,仍然会触发error事件,并且error变量包含模型(但没有错误消息)。
There should be a glitch in my syntax, or something I haven't understood, but when I am doing a save method on my model. The error callback is called whatever the outcome of the method.
@new_activity = new Activity()
@new_activity.save
know: $('input#know').val()
learn: $('input#learn').val()
success: -> console.log 'success'
error: -> console.log 'error'
In my case, since I do not really know whether the new_activity has effectively passed the validation, I have to do an ugly trick to add the activity to the collection. (By the way, I do not use the create method since I do want to have the different errors, and not a simple "false".
if @new_activity.has('know') and @new_activity.has('learn')
app.collections.activities.add @new_activity
When it is successful though; there is an alert of the created model.
Edit: Further details.
Here is my model:
initialize: ->
_.bindAll @, 'validate', 'errorHandler'
@.bind 'error', @errorHandler
validate: (attrs) ->
errors = []
# We only support a certain number of languages, we enforce that the user does not select a wrong set.
if _.isEmpty(_.intersection([attrs.know], ['en', 'fr'])) is true
errors.push 'This language is not currently supported.'
if _.isEmpty(_.intersection([attrs.learn], ['en', 'fr', 'de', 'es', 'zh', 'pt', 'ar', 'ja', 'ru'])) is true
errors.push 'You cannot learn this language yet.'
if _.isEmpty(errors) is false
errors
errorHandler: (model, error) ->
console.log error
When the validation occurs, and if the validate method returns nothing, it still triggers the error event, and the error variable contains the model (but no error message).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该检查new_activity是否正确保存,请验证服务器是否对PUT请求返回成功响应。
此外,我在使用 Rails 3.1 标准
format.json { head :ok }
时遇到了问题,因为它返回单个空格作为响应,而 application/json 作为内容类型。然后 Backbone 尝试解析 JSON 并因错误而终止。You should check whether the new_activity is saved properly, please verify that the server returns success response to the PUT request.
Furthermore, I have had issues with using Rails 3.1 standard
format.json { head :ok }
because it returns a single space as a reponse and application/json as content type. Backbone then tries to parse JSON and dies with an error.问题是 Rails 用
head :ok
发回的单个空格。如果您发回一个空字符串,Backbone 可以很好地处理它。正确的方法是发回
204 No Content
。您可以通过将format.json
块中的head :ok
替换为head :no_content
来实现此目的。 Rails 不会发回 HTTP 主体中的任何内容。The problem is the single space that Rails sends back with
head :ok
. If you send back an empty string, Backbone handles it fine.The proper way to do that is to send back a
204 No Content
. You can do that by replacinghead :ok
withhead :no_content
in theformat.json
block. Rails will not send back anything in the HTTP body.我不确定您的错误原因是什么,但我想我可以帮助您确定:
查看 Backbone.js 的带注释的源代码,您可以看到
save()
遵循sync()
,而后者又遵循 jQuery <一href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">$.ajax。因此最终,您的error
函数(由 Backbone 的wrapError
包装)将被调用。不幸的是,包装器丢弃了除 jqXHR 参数之外的所有参数,使得调试变得有点棘手。因此,您可能想尝试破解本地 Backbone.js 源文件,将现有的
wrapError
函数替换为如下所示:然后,至少,您将看到 jQuery 提供的所有调试数据你的控制台。
I'm not sure what the cause of your error is, but I think I can help you pin it down:
Looking at the annotated source for Backbone.js, you can see that
save()
defers tosync()
, which in turn defers to jQuery's $.ajax. So ultimately, yourerror
function (wrapped by Backbone'swrapError
) is called from that.Unfortunately, the wrapper discards all but the
jqXHR
argument, making debugging a bit tricky. So you might want to try hacking your local Backbone.js source file to replace the existingwrapError
function with something like this:Then, at least, you'll see all of the debugging data provided by jQuery on your console.