简体   繁体   English

通过hasMany关系访问ember-model对象中的相关集合

[英]access to a related collection in an ember-model object via hasMany relationship

I may fundamentally be misunderstanding how to use the hasMany relationships in Ember/ember-model. 我可能从根本上误会了如何在Ember / ember模型中使用hasMany关系。

the ember-model readme has the following example ember-model自述文件具有以下示例

postJson = {
  id: 99,
  title: 'Post Title',
  body: 'Post Body',
  comments: [
    {
      id: 1,
      body: 'comment body one',
    },
    {
      id: 2,
      body: 'comment body two'
    }
  ]
};

App.Post = Ember.Model.extend({
  id: Ember.attr(),
  title: Ember.attr(),
  body: Ember.attr(),
  comments: Ember.hasMany('App.Comment', {key: 'comments', embedded: true})
});

App.Comment = Ember.Model.extend({
  id: Ember.attr(),
  body: Ember.attr()
});

presumably, one would do the following 据推测,可以做到以下几点

post = App.Post.create();
post.load(1, postJson);

given the above, now we have access to various post props via get (ie post.get('title') ), but how to I access the comments? 鉴于以上所述,现在我们可以通过get(即post.get('title') )访问各种post道具,但是如何访问评论?

post.get('comments') returns an object, but it is not a collection of App.Comment objects, which is what I'd expect. post.get('comments')返回一个对象,但它不是App.Comment对象的集合,这是我期望的。

Thanks in advance for any and all help. 在此先感谢您提供的所有帮助。

It returns a collection object which is iterable, but not an array. 它返回一个可迭代的集合对象,但不是数组。 I'm working up an example with your code, I'll post it up momentarily (I'm pretty sure load is a private method and you should be using load on the model definition, then find). 我正在用您的代码编写一个示例,我会暂时将其发布(我很确定load是一个私有方法,您应该在模型定义上使用load然后找到)。

App.Post.load(postJson); //sideloading
return App.Post.find(99);

http://jsbin.com/hocopoga/1/edit http://jsbin.com/hocopoga/1/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM