Why is my Backbone.js callback called, although Rails supposedly returns a success response?

I am using Backbone.js (version 0.5.3) and I am having problems with a successful callback when saving the model . It does not start, although 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 model and response arguments, so this is success: (model, response) -> , doesn't matter. What is going wrong?

EDIT: As suggested by Trevor Burnham, I have added an error callback and it starts. So what should I return from a Ruby on Rails action in order for Backbone to account for 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'); }, error: function(model, response) { return console.log('in error'); } }); 

Here is the PUT request:

enter image description here

+7
source share
2 answers

I came across this. You cannot just return head :ok and use the default behavior for the baseline. By default, Backbone.Sync will not have it.

First of all, if you do this in your create action, you will never know what your id , so the model will not be able to update later (what you are doing, because of the β€œPUT”).

Secondly, in your update action, the model will not know that the data is really synchronized if you return head :ok so that synchronization ends again. But it doesn’t matter if you do not have id .

In any case, you need to return something in the body.

Rails scaffolding by default returns head :ok after a successful update . This is not related to Backbone. To fix this, return JSON instead:

 render json: @entity 

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

+12
source

The trunk expects JSON from the server, and the server returns with text.

To fix this on the client side, set the data type as text instead of json , and the server expects the content type as json .

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

Below is your updated code,

 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' }); 

Text data type will not be analyzed by Backbone after saving.

0
source

All Articles