How can y adjust json format?

i try to learn ember data. My json server for “Repos” is here : https://api.github.com/orgs/emberjs/repos the format json is not what the ember data except; i now !!! but how can y adjust it ?

my App.js:

       App = Ember.Application.create({ LOG_TRANSITIONS: true});

       App.Router.map(function(){
          this.resource("repos");
       });

       App.Store = DS.Store.extend({
           url: 'https://api.github.com/orgs/emberjs'
       });

       App.Repos = DS.Model.extend({
           name: DS.attr("string"),
           full_name: DS.attr("string")
       });

       App.ReposRoute = Ember.Route.extend({
           model: function() {
                  return this.store.find("repos");   
       }		
       });

And my index.html:

		<script type="text/x-handlebars" id="application">
			<h2> page application</h2>
		  {{#link-to "repos" }} repos {{/link-to}}
			{{outlet}}
		</script>
		
		<script type="text/x-handlebars" id="repos">
			 <h2> page Repos</h2>
			 <ul>
			{{#each}}
			<li> {{name}} </li>
			{{/each}}
			</ul>
		</script>

Please don’t tell me to look in officiel ember data guides !!! .

Thanks

You most likely wont be able to change the JSON structure GitHub outputs, so your best option is to write a custom adapter. Adapter - 4.6 - Ember API Documentation

I can’t change the json structure because i don’t have access to it .

Exactly, that’s why a custom adapter is the way to go.

ok,

I have a lost of frustration with ember data guides.

Can you propose any source example ? , please.

thanks.

You adjust the data in serializers. In your case I’d guess that ember-data isn’t finding the object type in the json.

You have

[
  { id: 1, [...] },
  { id: 2, [...] },
]

And what you (probably) want is,

{
  repos : [
    { id: 1, [...] },
    { id: 2, [...] },
  ]
}

So re-write the incoming json for Repos in ReposSerializer,

App.ReposSerializer = DS.RESTSerializer.extend({
  extractArray: function(store, type, payload, requestType) {
    payload = {
      repos: payload
    };
    return this._super(store, type, payload, requestType);
  }
);

“DS.RESTSerializer” - relevant API docs

1 Like