Skip to content

Instantly share code, notes, and snippets.

@fiznool
Created June 10, 2013 21:11
Show Gist options
  • Save fiznool/5752333 to your computer and use it in GitHub Desktop.
Save fiznool/5752333 to your computer and use it in GitHub Desktop.
Method to only send some Backbone Model attributes to server.
var UserModel = Backbone.Model.extend({
defaults: {
username: "",
password: {
old: "",
new: "",
confirm: ""
}
},
validate: function(attrs) {
if (attrs.password.new !== attrs.password.confirm) {
return "Passwords must match";
}
},
payload: function() {
var payload = this.toJSON();
delete payload.password.confirm;
return payload;
},
sync: function(method, model, options) {
if (method == 'update' || method == 'create') {
options.attrs = model.payload();
return Backbone.sync.call(model, method, model, options);
} else {
return Backbone.sync.call(this, method, this, options);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment