0
votes

I've got two Models using ember-data:

App.Post = DS.Model.extend({
    'comments': DS.hasMany('comment', {async: true})
});

App.Comment = DS.Model.extend({
    postId: DS.belongsTo('post', {async: true})
});

When I try to get the Posts via a Route

model: function(params) {
    return this.store.find('post', params.query);
}

Ember tries to find the Comments, which are not Part of the Posts Response from the API, although "async" is set to true.

Cannot read property 'comments' of undefined

Update

Some more detailed informations, complementing the information above:

window.App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: 'http://api.example.com'
});

App.ApplicationController = Ember.Controller.extend({
    actions: {
        search: function() {
            var term = this.get('query');
            this.transitionToRoute('post', { query: term });
        },
        reset: function() {
            clearTimeout(this.get("timeout"));
            this.setProperties({
                isProcessing: false,
                isSlowConnection: false
            });
        }
    }
});

App.Router.map(function() {
    this.resource('post', { path:'/:query' }, function(){
        this.route('create');
        this.route('edit');
    });
});

App.PostRoute = Ember.Route.extend({
    model: function(params) {
        return this.store.find('post', params.query);
    }
});

Data

"posts": [
    {
        "id": 1,
        "title": "Lorem ipsum",
        "comments": ["1", "2"],
    },
    {
        "id": 2,
        "title": "dolor sit amet",
        "comments": ["1", "2"],
    }
]

And

"comments": [
    {
        "id": "1",
        "body": "comment content 1",
        "postId": 1
    },
    {
        "id": "2",
        "body": "comment content 2",
        "postId": 1
    }
]
2
Are you trying to get comments that have already been stored on ember-data? - ShinySides
No, the comments aren't actually loaded, just the Posts - greg.ms

2 Answers

0
votes

If you wanna get information that is already loaded on the store you need to use

this.store.all('post')

And then use filter to get the information you want as an example

this.store.all('post').filterBy('key', value)

have a look at the documentation for further information on filtering if you need http://emberjs.com/guides/enumerables/

0
votes

As stated in a comment from ShinySides answer, Ember expects an object, instead of an array, because of the routing: if you got the route /posts the responded JSON has to be an Array with all posts:

"posts": [
    {
        "id": 1,
        "title": "Lorem ipsum",
        "comments": ["1", "2"],
    },
]

But for the route /posts/search-for-whatever-in-posts Ember expects an object, representing just one post:

"post": {
    "id": 1,
    "title": "Lorem ipsum",
    "comments": ["1", "2"],
},

The solution for my question: QUERYING FOR RECORDS:

var posts = this.store.find('posts', { term: "search for whatever in posts" });

which adds a parameter "term" to the API request (e.g. api.example.com/posts?term=whatever). Ember will now return an array.