When Backbone.js passes data to the server, it doesn't nest the CGI params in a model-name hash like Rails expects, so you have to pick them out of all the other params via Hash.select or a reverse merge or some other hack in the controller action, or use the emulateJSON hack in Backbone which JSON-encodes all the values in to a single "model" parameter and decode them in the controller. Is there a more elegant solution on either side?
1 Answer
You can override toJSON() in each Backbone Model so that it gives itself a root element. For example:
toJSON: function() {
return {modelname: _.clone(this.attributes)}
}
It is also easy to generalize this in a model base class, perhaps checking for the existence of a Model's 'jsonRoot' property or similar.