add paramRoot variable to Backbone.synch
So if youre using Rails like I am, you want all your request params nested under the name of the object. Its just messy to pick them off the root.
Rails wants
People.new params[:people]
Not
People.new :name => params[:name], :age => params[:age]
With a couple of likes of js, you can add this support to Backbone.synch
var orgSync = Backbone.sync;
Backbone.sync = function (method, model, options) {
var orgToJSON = model.toJSON;
if (_.contains(['create', 'update'], method)) {
model.toJSON = function () {
if (model.paramRoot) {
var data = {};
data[model.paramRoot] = _.clone(model.attributes);
return data;
} else {
return _.clone(model.attributes);
}
};
}
orgSync(method, model, options);
model.toJSON = orgToJSON;
};
this wraps in the value of paramRoot if there is one existant in the backbone model. but only on create and update just like you would expect.
The best part is that this allows the Backbone.synch definition to update and this shim will still work.
Written by David Morrow
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#