ember-data DS.RESTAdapter导致TypeError

时间:2012-04-27 18:44:27

标签: ember.js ember-data

我正在尝试使用ember-data的Ember.js,我定义了以下应用程序:

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

我无法弄清楚我做错了什么,在这种情况下,阅读源代码并不能帮助我解决问题。有没有人经历过这个,或者可以告诉我我的定义/定义不正确?

3 个答案:

答案 0 :(得分:2)

我可能遇到同样的问题over here。简而言之,ember-data期望资源列表嵌套在资源名称下,例如,如果获得/ users /,则结果应为{ "users": [] }而不是顶级数组[]

答案 1 :(得分:0)

您是否正在使用github master的ember / ember-data?这是由于过去几个月某些版本不兼容造成的。一旦我使用master,错误就会消失(以及许多其他错误)。

答案 2 :(得分: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);

如果这不适合你,请告诉我!