Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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');
  },
    error: function(model, response) {
    return console.log('in error');
  }
});

Here is the PUT request:

enter image description here

like image 849
ben Avatar asked Oct 06 '11 01:10

ben


People also ask

How does Backbone JS work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

Why use Backbone JS?

BackboneJS allows developing of applications and the frontend in a much easier way by using JavaScript functions. BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications.

What is Backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).


1 Answers

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)

like image 51
Brian Genisio Avatar answered Oct 21 '22 20:10

Brian Genisio