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

Already on GitHub? Sign in to your account

Is it possible to remove the root completely (to be compatible with backbone.js) #30

Closed
ramaman opened this Issue Jul 6, 2011 · 10 comments

Comments

Projects
None yet
4 participants

ramaman commented Jul 6, 2011

By default, acts_as_api renders a json response such as follows:

{
    "users": [
        {
            "last_name": "Skywalker",
            "first_name": "Luke"
        },
        {
            "last_name": "Solo",
            "first_name": "Han"
        },
        {
            "last_name": "Leia",
            "first_name": "Princess"
        }
    ]
}

Is it possible to render a backbone-js compatible format so it looks like this =>


[
    {
        "last_name": "Skywalker",
        "first_name": "Luke"
    },
    {
        "last_name": "Solo",
        "first_name": "Han"
    },
    {
        "last_name": "Leia",
        "first_name": "Princess"
    }
]

Thanks!

Owner

fabrik42 commented Jul 7, 2011

Please have a look at the ActsAsApi::Config.add_root_node_for option.

Another option would be to overwrite Backbone.Model.parse and Backbone.Collection.parse, depends what you like more :)

I hope I'll have some time on the weekend to put up some more docs and examples about using the lib with backbone.js :)

ramaman commented Jul 8, 2011

Thanks for the update!

To my understanding the 'ActsAsApi::Config.add_root_node_for' only works for adding root but not removing default ones? or I might be wrong here? In any case i'll just try to overwrite the 'Backbone.Model.parse' .

p.s. some docs on this topic would indeed be great, as backbone.js+rails combo is getting more popular these days :)

Rama

Contributor

vizjerai commented Sep 8, 2011

To get ActsAsApi to respond the way backbone.js expects add

config/initializers/acts_as_api.rb

ActsAsApi::Config.add_root_node_for = []

For Rails 3.0

config/initializers/active_record.rb

ActiveRecord::Base.include_root_in_json = false

For Rails 3.1 (this is the default)

config/initializers/wrap_parameters.rb

ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
end

Note: I'm not sure how rails 3.0 responds with missing root nodes so overriding the toJSON method may be needed. But Rails 3.1 will take care of it with wrap_parameters.

Owner

fabrik42 commented Sep 8, 2011

Thanks. Can I use this for a wiki article about this topic? Of course I'll give you credit!

Contributor

vizjerai commented Sep 8, 2011

Yeah, of course you can use it for a wiki article.

ramaman commented Sep 11, 2011

great, thanks alot guys!

I'm still curious how to do it on a case by case basis.

When I did (borrowing from vizjerai's global fix above):

format.json  { render_for_api :android_app, :json => quotes, :root => [] }

it gave something like

{"[]s":[{"created_at":"2011-06-09T11:21:28Z",

Also, I have Rails 3.0 and I can't seem to turn off these roots, even if I do

ActiveRecord::Base.include_root_in_json = false

(in such case I would not specify ":root" when calling render_for_api). Has anyone else experienced this?

Contributor

vizjerai commented Oct 26, 2011

Change the format to

format.json { render_for_api :android_app, :json => quotes }

What's going on is the root value is assuming it's a string and being pluralized and set as the root name.

Owner

fabrik42 commented Feb 11, 2013

Closing this for now. I think the problem has been solved

@fabrik42 fabrik42 closed this Feb 11, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment