错误回调总是解雇,即使是成功的 [英] Error callback always fired, even when it is successful

查看:143
本文介绍了错误回调总是解雇,即使是成功的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该有我的语法一个毛刺,或者说我没有理解,但是当我做我的模型保存方法。错误回调被调用的方法好歹。

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'

在我的情况,因为我真的不知道该new_activity是否已经有效地通过了验证,我要做一个丑陋的伎俩到活动添加到集合。 (顺便说一句,我不使用create方法,因为我想有不同的错误,而不是简单的假。

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.

编辑:的更多细节。
这里是我的模型:
    初始化: - >
        _.bindAll @,验证,的ErrorHandler
        @ .bind'错误',@errorHandler

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

验证时,如果validate方法返回什么都没有,它仍然会触发错误事件,并且错误变量包含模型(但没有错误消息)。

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

推荐答案

您应检查new_activity是否正确保存,请验证服务器返回成功响应PUT请求。

You should check whether the new_activity is saved properly, please verify that the server returns success response to the PUT request.

此外,我有问题与使用Rails 3.1的标准 format.json {头:OK} ,因为它返回一个空格作为一个作出反应并为application / json内容类型。骨干然后尝试解析JSON并以一个错误死亡。

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.

这篇关于错误回调总是解雇,即使是成功的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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