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
}
]