【发布时间】:2014-09-29 07:49:07
【问题描述】:
我有两个使用 ember-data 的模型:
App.Post = DS.Model.extend({
'comments': DS.hasMany('comment', {async: true})
});
App.Comment = DS.Model.extend({
postId: DS.belongsTo('post', {async: true})
});
当我尝试通过路由获取帖子时
model: function(params) {
return this.store.find('post', params.query);
}
Ember 尝试从 API 中查找不属于帖子响应的评论,尽管“async”设置为 true。
Cannot read property 'comments' of undefined
更新
一些更详细的信息,补充以上信息:
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);
}
});
数据
"posts": [
{
"id": 1,
"title": "Lorem ipsum",
"comments": ["1", "2"],
},
{
"id": 2,
"title": "dolor sit amet",
"comments": ["1", "2"],
}
]
和
"comments": [
{
"id": "1",
"body": "comment content 1",
"postId": 1
},
{
"id": "2",
"body": "comment content 2",
"postId": 1
}
]
【问题讨论】:
-
您是否要获取已存储在 ember-data 上的 cmets?
-
不,实际上并没有加载 cmets,只是加载了帖子
标签: ember.js ember-data