本文介绍了Ember-data,hasMany关系,加载子集合,嵌套路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用ember数据及其默认的REST适配器从rails后端加载模型。我有一个与另一个有很多关系的模型:

I need to load a model from a rails backend with ember-data and its default REST adaptor. I have a model that has many relationship with another one :

App.Post = DS.Model.extend({
    title: DS.attr("String");
    comments: DS.hasMany('App.Comment', {keys: 'post_ids', embbeded: true});
})

App.Post = DS.Model.extend({
    body: DS.attr("String");
    post: DS.belongsTo('App.Post');
})

服务器返回的JSON模型看起来像

The JSON model returned by the server looks like

{
  title: "a title",
  comment_ids: [1,2,3,4]
}

为了有效率,我需要首先加载所有的帖子,而不必加载相关的注释。我使用

I need at first to load all the posts without necessarly loading the associated comments, for efficiency reason. I do this with

App.store.findAll('App.Post');

而且,当我选择一个特定的帖子我需要加载所有的评论。在ember数据文档中,据说我只需要调用

And, when I select a specific post I need to load all the comments. In the ember-data documentation, it's said that I just need to call

a_specific_post.get('comments')

当我这样做时,我得到一个很长的URL,所有的评论ids:

When, I do this I get a very long url with all comment ids :

GET : /comments?ids%all_ids_appended_here

当然这不行,如果我有一千个评论,网址很长。

Of course it doesn't work and if I have a thousand of comment the url is very very long.

可以获得匹配rails的嵌套路由模型的请求吗? :

Is it possible to get a request that matches nested routing model of rails ? :

 GET /posts/post_id/comments

插件路由管理器似乎是这样的路由。我可以用ember数据和如何使用它?

The plugin route-manager https://github.com/ghempton/ember-routemanager seems to this kind of routing. Can I use it with ember-data and how ?

谢谢

推荐答案

p>如果您只指定注释ID,我不确定嵌入式是你想要的。期待完整的注释对象在post模型的JSON表示中被传递。

I'm not sure embedded is what you want if you're only specifying comment IDs. It is expecting full comment objects to be passed in the JSON representation of the post model.

这篇关于Ember-data,hasMany关系,加载子集合,嵌套路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 10:54