codebrew / backbone-rails

Easily use backbone.js with rails 3.1
MIT License
1.62k stars 255 forks source link

On model save, using backbone_rails_sync and {patch: true}, the entire model's attributes are sent to Rails. #198

Closed reidcooper closed 4 years ago

reidcooper commented 8 years ago

https://github.com/codebrew/backbone-rails/blob/master/vendor/assets/javascripts/backbone_rails_sync.js

When passing {patch: true} to a model save, typically Backbone should be sending only the changed attributes to the server. Now when using the backbone_rails_sync.js override, using {patch: true} does not send the changed attributes but sends the entire model to rails. I am guessing during the override, backbone may think that the entire model attributes have been changed, therefore sending those values? When I do not use backbone_rails_sync.js, Backbone sends the proper changed attributes.

jmadureira commented 8 years ago

Actually looking at the code it's treating create, update and patch the exact same way. I suppose that for patch it should be calling changedAttributes instead of calling toJSON for the entire model.

jmadureira commented 8 years ago

I was able to get this running with the following code:

if ((options.data == null) && model && (method === 'create' || method === 'update' || method === 'patch')) {
  options.contentType = 'application/json';
  data = {};
  if (model.paramRoot) {
    data = {};
    data[model.paramRoot] = options.attrs || model.toJSON(options);
  } else {
    data = options.attrs || model.toJSON(options);
  }
  options.data = JSON.stringify(data);
}

Haven't had the time to test it more thoroughly but so far all test from my app are passing...

jmadureira commented 8 years ago

Turns out this is a duplicate of #188.