ember-data DS.RESTAdapter causes TypeError

…衆ロ難τιáo~ 提交于 2020-01-14 14:32:50

问题


I'm trying out Ember.js with ember-data, and I have the following application defined:

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'

That template looks like this:

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

When I try to load App.ListSourcesView in my page, I get an error in ember-data.js saying: Uncaught TypeError: Cannot read property 'map' of undefined.

I can't figure out what I'm doing wrong, and reading through the source isn't helping my confusion any in this case. Has anyone experienced this, or can tell me what I've defined/not defined incorrectly?


回答1:


Possibly the same thing I ran into over here. In short ember-data expects resource lists to be nested under the resource name, eg if you get /users/ then the result should be { "users": [] } and not a top-level array [].




回答2:


Are you using ember/ember-data from github master? This is caused by an incompatibility in some versions over the past few months. Once I used master for both the error went away (along with lots of other bugs).




回答3:


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

Let me know if this doesn't work out for you!



来源:https://stackoverflow.com/questions/10356129/ember-data-ds-restadapter-causes-typeerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!