Skip to content

Instantly share code, notes, and snippets.

@sporkd
Created June 20, 2011 22:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sporkd/1036796 to your computer and use it in GitHub Desktop.
Save sporkd/1036796 to your computer and use it in GitHub Desktop.
Alias Backbone.sync method to inject Rails specific parameters
_.extend(Backbone.Model.prototype, {
// By default, rails expects model params to be wrapped in a nested params hash
// https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/params_wrapper.rb
_paramsWrapper : function() {
return this.paramsWrapper;
}
});
// Alias Backbone.sync method to inject Rails specific
// parameters into the options.data
Backbone.sync = (function(original){
return function(method, model, options) {
// Inject params data inside the options hash
if (!options.data && model && (method == 'create' || method == 'update' || method == 'delete')) {
var railsParams = {};
if (method == 'create' || method == 'update') {
// We can set contentType to JSON
options.contentType = 'application/json';
// Wrap model attributes if paramsWrapper key is defined
if (this.paramsWrapper) {
railsParams[this.paramsWrapper] = _.clone(this.attributes);
}
else {
railsParams = _.clone(this.attributes);
}
}
// Add rails csrf params
csrfParm = $("meta[name='csrf-param']").attr('content');
csrfToken = $("meta[name='csrf-token']").attr('content');
if (!csrfParm || !csrfToken) {
throw new Error("Did you forget to set csrf_meta_tag in your rails layout?");
}
railsParams[csrfParm] = csrfToken;
// Add rails utf8 param
if (method == 'create' || method == 'update') {
railsParams['utf8'] = "✓";
}
options.data = JSON.stringify(railsParams);
}
// Call aliased Backbone.sync with our options
original(method, model, options);
};
})(Backbone.sync);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment