在ember中访问模特儿童的最佳方式

时间:2014-01-24 11:40:29

标签: ember.js

在路线中访问模特儿童的最佳/正确方法是什么?

路线代码:

this.store.find('Parent', params.parent_id).then(function(parent){
   //this works
   var a = parent._data.children.length;
   //this doesn't but feels like it should?
   var a = parent.get('children').length;
})

型号:

App.Parent = DS.Model.extend({
    ...
    children: DS.hasMany('child', {async: true}),
});

1 个答案:

答案 0 :(得分:1)

var a = parent.get('children').length;

可能会回复一个承诺。您可以通过执行console.log('children', parent.get('children')来检查此问题。您可以在通过以下方式检查长度之前解决此承诺:

var a;
parent.get('children').then(function (children) {
  // Children promise has been resolved
  a = children.length;
});