Error callback always fired, even when it is successful

荒凉一梦 提交于 2019-12-13 17:05:32

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7090202/error-callback-always-fired-even-when-it-is-successful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!