Last Updated: February 25, 2016
· dperrymorrow

add paramRoot variable to Backbone.synch

So if youre using Rails like I am, you want all your request params nested under the name of the object. Its just messy to pick them off the root.

Rails wants params[:people]

Not :name => params[:name], :age => params[:age]

With a couple of likes of js, you can add this support to Backbone.synch

var orgSync = Backbone.sync;
Backbone.sync = function (method, model, options) {

  var orgToJSON = model.toJSON;
  if (_.contains(['create', 'update'], method)) {

    model.toJSON = function () {
      if (model.paramRoot) {
        var data = {};
        data[model.paramRoot] = _.clone(model.attributes);
        return data;
      } else {
        return _.clone(model.attributes);

  orgSync(method, model, options);
  model.toJSON = orgToJSON;

this wraps in the value of paramRoot if there is one existant in the backbone model. but only on create and update just like you would expect.

The best part is that this allows the Backbone.synch definition to update and this shim will still work.