【问题标题】:async: true still requires associated Model in Emberasync: true 仍然需要 Ember 中的关联模型
【发布时间】: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


【解决方案1】:

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

this.store.all('post')

然后以过滤器获取你想要的信息为例

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

如果需要,请查看文档以获取有关过滤的更多信息 http://emberjs.com/guides/enumerables/

【讨论】:

  • cmets 尚未加载,只是帖子,所以我需要“找到”,以请求 API
  • 是的,但如果你这样做了 this.store.find('post') 它不会加载 cmets,你需要在 post 对象上执行 get .get('cmets') 。跨度>
  • 是的,但这应该在点击后发生:如果您加载页面,所有帖子都是可见的,稍后,如果用户点击链接,例如通过特定帖子“显示 cmets”,评论应通过 Ajax 加载。
  • 对不起现在我明白你的意思了,看代码好像没问题,你有意见吗?或者在使用 cmets 的控制器上还有其他方法吗?如果您可以发布完整的代码,就会更容易看到问题。
  • 有一个空的View,只是一个没有功能的脚手架。在我上面的问题中,您可以看到整个代码。
【解决方案2】:

正如 ShinySides 回答的评论中所述,Ember 需要一个对象,而不是一个数组,因为路由:如果你得到路由 /posts 响应的 JSON 必须是一个包含所有帖子的数组:

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

但是对于路由 /posts/search-for-whatever-in-posts Ember 需要一个对象,只代表一个帖子:

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

我的问题的解决方案:QUERYING FOR RECORDS:

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

向 API 请求添加参数“term”(例如 api.example.com/posts?term=whatever)。 Ember 现在将返回一个数组。

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多