2012-04-27 67 views
3

我嘗試Ember.js與Ember數據,和我有以下應用程序定義:燼數據DS.RESTAdapter導致類型錯誤

window.App = Ember.Application.create() 

App.store = DS.Store.create 
    revision: 4 
    adapter: DS.RESTAdapter.create { bulkCommit: false } 

App.Source = DS.Model.extend 
    # options 
    primaryKey: '_id' 

    # fields 
    name: DS.attr 'string' 
    raw_text: DS.attr 'string' 

App.sourcesController = Ember.ArrayProxy.create 
    content: App.store.findAll App.Source 

App.ListSourcesView = Ember.View.extend 
    templateName: 'app/templates/sources/list' 
    sourcesBinding: 'App.sourcesController' 

該模板看起來是這樣的:

<table> 
    <thead> 
     <tr> 
      <th>ID</th> 
      <th>Name</th> 
     </tr> 
    </thead> 
    <tbody> 
     {{#each sources}} 
     {{view App.ShowSourceView sourceBinding="this"}} 
     {{/each}} 
    </tbody> 
</table> 

當我嘗試在我的頁面中加載App.ListSourcesView時,在ember-data.js中出現錯誤:Uncaught TypeError: Cannot read property 'map' of undefined

我不知道我做錯了什麼,在這種情況下閱讀源代碼並沒有幫助我的困惑。有沒有人遇到過這種情況,或者可以告訴我我定義/沒有正確定義的內容?

+0

我爲您的代碼創建了一個JSFiddle,它的工作原理如下:http://jsfiddle.net/pangratz666/qySE9/。 – pangratz 2012-05-03 07:14:59

回答

2

可能是我碰到over here相同的東西。簡而言之,燼數據期望資源列表嵌套在資源名稱下,例如,如果您得到/ users /,那麼結果應該是{ "users": [] }而不是頂級數組[]

+0

我真的結束了使用淘汰賽。這很可能。 – 2012-05-29 15:26:24

+0

使用rails提供數據時,'active_model_serializers'是最好的開始。 – 2012-09-13 08:54:57

0

您是否使用github master的ember/ember-data?這是由於過去幾個月中某些版本的不兼容造成的。一旦我使用了master,這個錯誤就消失了(還有很多其他的bug)。

+0

我希望它是如此!我嘗試了兩種方法,仍然遇到了錯誤。我想我只需要等到ember-data稍微成熟後再使用ember。 – 2012-05-02 20:45:46

0
App.sourcesController = Ember.ArrayProxy.create 
    content: [] 

App.ListSourcesView = Ember.View.extend 
    // I don't believe you can use file paths in this way 
    // Use a script tag with a data-template-name as shown in the docs. 
    // If you really need them from a file, then you will need to compile the templates 
    // Yourself. There's plenty of SO Questions re: this. 
    templateName: 'source-list' 
    sourcesBinding: 'App.sourcesController' 

// Always access ember object properties through the get and set interfaces. 
// This will ensure that views get the proper notifications when props change. 
App.sourcesController.set("content", App.store.findAll App.Source); 

讓我知道如果這不適合你!

相關問題