Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js async:true仍然需要Ember中的关联模型_Ember.js_Ember Data - Fatal编程技术网

Ember.js async:true仍然需要Ember中的关联模型

Ember.js async:true仍然需要Ember中的关联模型,ember.js,ember-data,Ember.js,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.sto

我有两个使用余烬数据的模型:

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);
}
尽管async设置为true,但Ember尝试查找注释,这些注释不是来自API的POST响应的一部分

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);
    }
});
资料


如果你想获得商店中已经加载的信息,你需要使用

this.store.all('post')
然后使用过滤器获取所需的信息作为示例

this.store.all('post').filterBy('key', value)
如果需要,请查看文档以获取有关筛选的更多信息

正如ShinySides answer的评论所述,由于路由,Ember需要一个对象而不是一个数组:如果您得到路由/帖子,响应的JSON必须是一个包含所有帖子的数组:

"posts": [
    {
        "id": 1,
        "title": "Lorem ipsum",
        "comments": ["1", "2"],
    },
]
但是对于route/posts/search for which in posts,Ember需要一个对象,仅表示一篇文章:

"post": {
    "id": 1,
    "title": "Lorem ipsum",
    "comments": ["1", "2"],
},
我的问题的解决方案是:


它在API请求中添加了一个参数项,例如API.example.com/posts?term=which。Ember现在将返回一个数组。

您是否正在尝试获取已存储在Ember数据上的注释?不,注释没有实际加载,只是帖子注释还没有加载,只是帖子,所以我需要查找,以请求APIyes,但如果您这样做。store.find'post'则不会加载注释,你需要在一个post对象上进行get.get'comments'操作。是的,但这应该在以后单击时发生:如果你加载页面,所有的帖子都是可见的,然后,如果用户单击一个链接,例如,通过特定的帖子显示评论,评论应该通过Ajax加载。对不起,现在我明白你的意思了,看看代码,它看起来很好,你有视野吗?或者控制器上是否有其他使用注释的方法?如果你能发布完整的代码,你会更容易看到这个问题。有一个空的视图,只有一个没有函数的脚手架。在我上面的问题中,您可以看到整个代码。
"posts": [
    {
        "id": 1,
        "title": "Lorem ipsum",
        "comments": ["1", "2"],
    },
]
"post": {
    "id": 1,
    "title": "Lorem ipsum",
    "comments": ["1", "2"],
},
var posts = this.store.find('posts', { term: "search for whatever in posts" });