0

I have a RESTful JSON api that I use to perform server-side calls like this:

Servlet.prototype.ajaxJSON = function (jobject, func, context) {
    var self = this;
    $.getJSON(this.name, jobject, function (json) {

        ...

    }).fail(function(jqXHR, status, errorThrown) {
        var callname = JSON.stringify(jobject).slice(1,JSON.stringify(jobject).indexOf(':'));

        if(func !== null) {
            func(JSON.parse('{' + callname+': {"error": "Server Error:' + errorThrown + '"}}'));
        }
    });
};

However, when I try to use the error callback in my model:

newComment.save(null, {
    'success': _.bind(function(model, response) {

        ...

    }, this),
    'error': function(model, error) {
        errorAlert(error, 'Could not post comment');
    }
});

For some reason, I'm getting a Backbone model for my error parameter. I've stepped through the code and it looks like Backbone has some sort of custom wraperror method that's screwing everything up. Can anyone tell me what is going on here? Thanks!

brokethebuildagain
  • 2,162
  • 1
  • 22
  • 44

1 Answers1

0

Figured it out. The problem was with my model.sync method. I had a condition in it to check for an error that looked like:

if(_.isObject(json.post_comment) && json.post_comment.error) {
    options.error(model, json.post_comment.error, options);
}

That needed to be:

if(_.isObject(json.post_comment) && json.post_comment.error) {
    options.error(json.post_comment.error);
}

Guess I was reading the documentation wrong. :/

brokethebuildagain
  • 2,162
  • 1
  • 22
  • 44