Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send changedAttributes to Backbone.sync #391

Closed
wants to merge 1 commit into from
Closed

Send changedAttributes to Backbone.sync #391

wants to merge 1 commit into from

Conversation

gblache
Copy link

@gblache gblache commented Jun 2, 2011

  • When changedAttributes is called from within Backbone.sync
    it returns false as the change method overwrites
    _previousAttributes. This makes it hard to send only
    the updated attributes to the server when a model is
    updated.

  * When changedAttributes is called from within Backbone.sync
    it returns false as the change method overwrites
    _previousAttributes.  This makes it hard to send only
    the updated attributes to the server when a model is
    updated.
@eoin
Copy link

eoin commented Jun 7, 2011

I'd love to see this merged.

@XiXora
Copy link

XiXora commented Jun 7, 2011

Sending only updated attributes? Sounds like PATCH method to me. :)
http://tools.ietf.org/html/rfc5789

@jashkenas
Copy link
Owner

This has been discussed several times before -- check the issues. Backbone by default PUTs the full representation of the model -- if you'd like to only send the change set, feel free to override sync or save.

@jashkenas jashkenas closed this Jul 1, 2011
@eoin
Copy link

eoin commented Jul 1, 2011

Understood, however overriding sync to only send changed attributes is currently quite tricky. This change would make it much more straightforward.

@matjaz
Copy link

matjaz commented Dec 6, 2011

This is indeed issue. Calling changedAttributes() inside CUSTOM sync ALWAYS returns false.

@walling
Copy link

walling commented Mar 31, 2012

At least an example how to go about doing this in a custom sync would be preferable.

@mattdiamond
Copy link

This could work as a makeshift solution:

function sync (method, model, options){
  var newData = {};
  for (var prop in options.changes){
    if (options.changes.hasOwnProperty(prop)){
      newData[prop] = model.get(prop);
    }
  }
  /* save newData to the server */
}

@paulcollinsiii
Copy link

Another way of kinda doing the same thing:

/**
 * This assumes we're just overriding the sync method of a single model
 * and that we're only concerned with poking at changed data for update
 * events sent to the server
 */
sync: function (method model options){
  if(method === "update"){
     var changedData = {};
     _.each(options.changes, function(changed, key){
          if(changed){ changedData[key] = model.get(key); }
     });
     // If you set data, you need to declare your content type
     // Note I use PATCH because my backend uses tastypie
     // it's not strictly required though
     options = _.extend(options, {type: "PATCH",
                                  contentType: 'application/json',
                                  data: JSON.stringify(changedData)});
   }
   Backbone.sync(method, model, options);
} 

The "sneaky" bit here is putting data in the options. Keep in mind that if you have to do emulateJSON that changes what you'll have to set your options.data to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants