2012-04-27 66 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); 

让我知道如果这不适合你!

相关问题