Backbone.js syncs all model data to the server by default regardless of what actually changed. This makes a lot of frontend tasks and debugging much easier, but for some models, sending all the data every time is overkill.
Here’s a quick extension to backbone models and collections to only sync changes. Just call model.saveChanges instead of model.save and only your changes will be synced.
CoffeeScript
12345678910111213141516
cx_backbone_common =sync: (method, model, options) -># Changed attributes will be available here if model.saveChanges was called instead of model.saveifmethod=='update'&&model.changedAttributes()options.data = JSON.stringify(model.changedAttributes())options.contentType = 'application/json';Backbone.sync.call(this,method,model,options)cx_backbone_model =# Calling this method instead of set will force sync to only send changed attributes# Changed event will not be triggered until after the model is syncedsaveChanges: (attrs) ->@save(attrs,{wait: true})_.extend(Backbone.Model.prototype,cx_backbone_common,cx_backbone_model)_.extend(Backbone.Collection.prototype,cx_backbone_common)