Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespacing outgoing requests w/ Rails convention #117

Closed
jordan-brough opened this issue Dec 3, 2010 · 9 comments
Closed

Namespacing outgoing requests w/ Rails convention #117

jordan-brough opened this issue Dec 3, 2010 · 9 comments

Comments

@jordan-brough
Copy link

(note: this is different than the "include_root_in_json" question)

Is there a way to namespace attributes on the outgoing PUT/POST requsts that backbone makes? e.g. to follow the Rails convention of:

account.update_attributes params[:account]

rather than

account.update_attributes params

?

We're integrating backbone into our existing app which has a bunch of endpoints following the former convention. If there's not a way right now would you be willing to accept a patch for it?

@jordan-brough
Copy link
Author

possibly something like https://gist.github.com/7d0a00c2c03e5a053144 ?

@collin
Copy link

collin commented Dec 3, 2010

Why not just override toJSON on your own models?

@jashkenas
Copy link
Owner

I think I agree with collin about this for the time being. Adding a line like:

jsonRoot: "account"

is only slightly easier than doing this:

toJSON : function(){ return {account: _.clone(this.attributes)}; } 

Let's leave things as they are for the time being, and if this arises as a common question, we can add an option for it.

Also note that you can accomplish the same thing for all of your models at once by overriding Backbone.sync.

@jordan-brough
Copy link
Author

Won't that mess up the collection toJSON though? Seems a bit weird to have the endpoint config tweak up the json representation of things...?

@jashkenas
Copy link
Owner

It depends on what your endpoint is expecting. Collection#toJSON isn't used internally by Backbone for anything.

If you'd like to discuss this in a livelier fashion, feel free to pop in to #documentcloud

@jordan-brough
Copy link
Author

Thanks for the IRC chat. We'll try overriding toJSON or Backbone.sync.

For posterity:

Also note that you can accomplish the same thing for all of your models at once by overriding Backbone.sync.

Since the models don't know their own names we can't just tweak Backbone.sync and be done -- we have to go through and configure the models to at least know their names. Which is doable of course.

@kendagriff
Copy link

Putting the solution for this into the documentation would be really nice. Awesome framework, by the way.

@jashkenas
Copy link
Owner

If someone who's using Backbone with a Rails app in the "standard" style wants to put together a paragraph detailing the best way to get things integrated -- I'd be glad to drop it in the FAQ on the homepage.

@maletor
Copy link

maletor commented Dec 19, 2011

Please reopen this issue. There should either be documentation on how to set this up with the Rails conventions, which are logical and sane defaults. (No one wants an update action for every little attribute on a model, just one update action is fine thank you.) Or this should be reviewed to go into core https://gist.github.com/7d0a00c2c03e5a053144

@jashkenas jashkenas reopened this Dec 19, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants