3

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

3 Answers 3

2

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 [].

Sign up to request clarification or add additional context in comments.

2 Comments

I actually ended up using knockout. This is very likely it.
When serving data with rails, active_model_serializers is the good point to start from.
0

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

1 Comment

I wish it were so! I tried both from master and still got the error. I guess I'll just have to wait to use ember until ember-data matures a bit.
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);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.