2014-02-19 37 views
0

我設置了一個API,並試圖使用Ember.js DS.RESTAdapter來獲取並遍歷表中的所有記錄。目前它的部分工作正在進行,但並沒有得到所有的記錄,我只能得到一個(最後一個記錄是特定的)。所以在下面的代碼中,返回的模型的長度是1,所有列出的都是Gale Sayers。 JSON有什麼問題嗎?這是多數民衆贊成由API返回的JSON:Ember.js find()只返回一個項目

{ 
    "users":[ 
     { 
      "id":{"$oid":"52f94fc6477261d1a9110000"}, 
      "first_name":"Jim", 
      "last_name":"Browne", 
      "email":"[email protected]" 
     }, 
     { 
      "id":{"$oid":"52f4088c477261b72a000000"}, 
      "first_name":"Gale", 
      "last_name":"Sayers", 
      "email":"[email protected]" 
     } 
    ] 
} 

僅供參考,這裏是我的Ember.js代碼中的相關部分:

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'); 
    } 
}); 

最後的模板文件:

application.handlebars

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

個index.handlebars

Length: {{model.length}} 
<ul> 
{{#each model}} 
    <li>{{first_name}} {{last_name}}</li> 
{{/each}} 
</ul> 
+1

我認爲問題在於,您使用的是ids對象。嘗試將ID更改爲:「ID」:「52f94fc6477261d1a9110000」。 – Martin

回答

0

馬丁在他的問題意見是正確的。來自Mongoid的對象id是問題所在。我發現了另一個相同的問題和一個很好的解決方案stackoverflow帖子:Ember model only loads last record