martini-api-example icon indicating copy to clipboard operation
martini-api-example copied to clipboard

Adding a root object to the JSON output

Open hourback opened this issue 9 years ago • 3 comments

Hi there! Another Ember.js-related issue. :-) Just wondering if you can give me an idea of the most Go-like or Martini-like way to handle this.

Here's a related post on Stack Overflow:

http://stackoverflow.com/a/18650879/16609

Basically, Ember's RESTAdapter expects the JSON from the server to contain a root object that is the same name as the model. I'm unsure how to modify your API example to accommodate this requirement. (I'm new to Go.) Maybe changing the functions that pull the data, e.g., GetAll(), is the easiest, as opposed to monkeying with the encoder? I think there will be additional types/models in the future so it seems like the encoders should be as generic as possible.

TIA,

Ali

hourback avatar Sep 25 '14 16:09 hourback

Just had another thought. Adding another argument (a JSON root object name) to the encoder might be a good way of handling this.

hourback avatar Sep 25 '14 16:09 hourback

That's a weird requirement from Ember... So you can't return an object like {"key": "value"}, you have to have {"model_name": {"key":"value"}}? And when you return an array, it must be {"model_name": [...]}?

mna avatar Sep 25 '14 20:09 mna

@PuerkitoBio , yeah, that appears to be the default behavior when dealing with JSON from a REST API. You can override it, apparently, which I might try doing first.

http://emberjs.com/guides/models/the-rest-adapter/#toc_json-root

hourback avatar Sep 25 '14 21:09 hourback