错误回调总是被触发,即使它成功了

发布于 2024-11-30 03:43:55 字数 1396 浏览 1 评论 0原文

我的语法中应该有一个小故障,或者我不理解的东西,但是当我在我的模型上执行保存方法时。无论该方法的结果如何,都会调用错误回调。

    @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年已逝 2024-12-07 03:43:55

您应该检查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.

淡写薰衣草的香 2024-12-07 03:43:55

问题是 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 replacing head :ok with head :no_content in the format.json block. Rails will not send back anything in the HTTP body.

恬淡成诗 2024-12-07 03:43:55

我不确定您的错误原因是什么,但我想我可以帮助您确定:

查看 Backbone.js 的带注释的源代码,您可以看到 save() 遵循 sync(),而后者又遵循 jQuery <一href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">$.ajax。因此最终,您的 error 函数(由 Backbone 的 wrapError 包装)将被调用。

不幸的是,包装器丢弃了除 jqXHR 参数之外的所有参数,使得调试变得有点棘手。因此,您可能想尝试破解本地 Backbone.js 源文件,将现有的 wrapError 函数替换为如下所示:

var wrapError = function(onError, model, options) {
    return function(jqXHR, textStatus, errorThrown) {
      console.log(jqXHR);
      console.log(textStatus);
      console.log(errorThrown);
      if (onError) {
        onError(model, jqXHR, options);
      } else {
        model.trigger('error', model, jqXHR, options);
      }
    };
  };

然后,至少,您将看到 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 to sync(), which in turn defers to jQuery's $.ajax. So ultimately, your error function (wrapped by Backbone's wrapError) 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 existing wrapErrorfunction with something like this:

var wrapError = function(onError, model, options) {
    return function(jqXHR, textStatus, errorThrown) {
      console.log(jqXHR);
      console.log(textStatus);
      console.log(errorThrown);
      if (onError) {
        onError(model, jqXHR, options);
      } else {
        model.trigger('error', model, jqXHR, options);
      }
    };
  };

Then, at least, you'll see all of the debugging data provided by jQuery on your console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文