簡體   English   中英

通過hasMany關系訪問ember-model對象中的相關集合

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

我可能從根本上誤會了如何在Ember / ember模型中使用hasMany關系。

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()
});

據推測,可以做到以下幾點

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

鑒於以上所述,現在我們可以通過get(即post.get('title') )訪問各種post道具,但是如何訪問評論?

post.get('comments')返回一個對象,但它不是App.Comment對象的集合,這是我期望的。

在此先感謝您提供的所有幫助。

它返回一個可迭代的集合對象,但不是數組。 我正在用您的代碼編寫一個示例,我會暫時將其發布(我很確定load是一個私有方法,您應該在模型定義上使用load然后找到)。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM