Ember:访问有很多关系数组

时间:2015-01-13 12:10:40

标签: ember.js ember-data

我有,说:

App.Post = DS.Model.extend({
  comments: DS.hasMany('App.Comment')
});

App.Comment = DS.Model.extend({
  post: DS.belongsTo('App.Post')
});

在某些代码中,我检索post并且我想循环其注释:

var comment;
var post = store.all('post').content[0];
for (var i=0; i<post.get('comments').length; i++) { // length attributes exists
  // comment is undefined
  comment = post.get('comments')[i]; // also tried post.get('comments').get(i);
}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

诀窍是一个toArray()方法,感谢这个post,虽然我从来没有在文档中找到它:

var comment;
var post = store.all('post').content[0];
var comments = post.get('comments').toArray();
for (var i=0; i<comments.length; i++) { // length attributes exists
  comment = comments[i];
}