1

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 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.