Question

I've set up an API and am trying to use the Ember.js DS.RESTAdapter to get and iterate through all the records in a table. At the moment it's partially working, but instead of getting all of the records I'm getting only one (the last record to be specific). So with the code below, the length of the model being returned is 1 and all that's being listed is Gale Sayers. Is there something wrong with the JSON? Here is the JSON that's being returned by the API:

{
    "users":[
        {
            "id":{"$oid":"52f94fc6477261d1a9110000"},
            "first_name":"Jim",
            "last_name":"Browne",
            "email":"user186248@example.com"
        },
        {
            "id":{"$oid":"52f4088c477261b72a000000"},
            "first_name":"Gale",
            "last_name":"Sayers",
            "email":"user186248@example.com"
        }
    ]
}

For reference, here's the relevant portion of my Ember.js code:

window.App = Ember.Application.create({
    rootElement: '#ember-app',
    Resolver: Ember.DefaultResolver.extend({
      resolveTemplate: function(parsedName) {
        parsedName.fullNameWithoutType = "app/" + parsedName.fullNameWithoutType;
        return this._super(parsedName);
      }
    })
});

App.ApplicationAdapter = DS.RESTAdapter.extend({
    namespace: 'api'
});

App.Store = DS.Store.extend({
    adapter: App.ApplicationAdapter
});

App.User = DS.Model.extend({
    first_name: DS.attr('string'),
    last_name: DS.attr('string'),
    email: DS.attr('string')
});

App.Router.map(function() {
});

App.ApplicationRoute = Ember.Route.extend({
});

App.IndexRoute = Ember.Route.extend({
    model: function() {
      return this.store.find('user');
    }
});

And finally the template files:

application.handlebars

<b>Ember is working!</b>
{{outlet}}

index.handlebars

Length: {{model.length}}
<ul>
{{#each model}}
    <li>{{first_name}} {{last_name}}</li>
{{/each}}
</ul>
Était-ce utile?

La solution

Martin was correct in his comment to the question. The object ids coming through from Mongoid were the problem. I found another stackoverflow post with the same problem and a good solution: Ember model only loads last record.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top