开发者

Why is my Backbone.js error callback being called even though Rails is supposedly returning a success response?

开发者 https://www.devze.com 2023-04-10 19:59 出处:网络
I am using Backbone.js (version 0.5.3) and am having some trouble with a success callback when saving a model. It isn\'t being run, even though the model is successfully saved on the server.

I am using Backbone.js (version 0.5.3) and am having some trouble with a success callback when saving a model. It isn't being run, even though the model is successfully saved on the server.

CoffeeScript:

console.log 'in switch_private'
console.log "private_entry attribute is currently #{@model.get('private_entry')}"
@model.save {'private_entry': true},
  success: ->
    console.log 'in success'

Compiled Javascript:

console.log('in switch_private');
console.log("private_entry attribute is currently " + (this.model.get('private_entry')));
return this.model.save({
  'private_entry': true
}, {
  success: function() {
    return console.log('in success');
  }
});

Console output:

in switch_private
private_entry attribute is currently false
XHR finished loading: "http://localhost:3000/entries/235".

I am returning head :ok from the update action in Ruby on Rails.

Adding the model and response arguments, so that it's success: (model, response) ->, doesn't make a difference. What is going wrong?

EDIT: As per Trevor Burnham's suggestion, I added an error callback, and it is being run. So what should I be returning from the Ruby on Rails action in order for Backbone to consider the save a success? At the moment I have head :ok

EDIT 2: Here is my updated compiled Javascript:

var this_view;
this_view = this;
return this.model.save({
  'private_entry': !(this.model.get('private_entry'))
}, {
  success: function(model, response) {
    return console.log('in success');
  },
    err开发者_如何学运维or: function(model, response) {
    return console.log('in error');
  }
});

Here is the PUT request:

Why is my Backbone.js error callback being called even though Rails is supposedly returning a success response?


I've run into this. You can't just return head :ok and use Backbone's default behavior. The default Backbone.Sync won't have it.

First of all, if you are doing it in your create action, you won't ever know what your id is so the model won't be able to update later (which you are doing, because of the "PUT").

Second, in your update action, the model won't know that the data is truly in sync if you return head :ok so the sync fails again. But is doesn't matter if you don't have an id.

Whatever the case, you need to return something in the body.

Rails scaffolding, by default, returns head :ok upon successful update. This doesn't jive with Backbone. To fix it, return the JSON instead:

render json: @entity

(where @entity is whatever your variable is in the action)


Backbone expects the JSON from the server, and the server returns with a text.

To fix this at client side, set the data type as text instead of json, and the server is expecting content type as json.

options.contentType = 'application/json';
options.dataType = 'text'; 

Your Updated code follows,

return this.model.save({'private_entry': !(this.model.get('private_entry'))}, {
     success: function(model, response) {
        return console.log('in success');
     },
     error: function(model, response) {
        return console.log('in error');
     }, 
     contentType : 'application/json',
     dataType : 'text'
});

The datatype text will not be parsed by Backbone after save.

0

精彩评论

暂无评论...
验证码 换一张
取 消