Reputation: 2737
What is the best/right way to access a model's children in a route?
Route Code:
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;
})
Model:
App.Parent = DS.Model.extend({
...
children: DS.hasMany('child', {async: true}),
});
Upvotes: 0
Views: 76
Reputation: 1717
var a = parent.get('children').length;
Is probably returning a promise. You can check this by doing a console.log('children', parent.get('children')
. You can resolve this promise before checking the length by:
var a;
parent.get('children').then(function (children) {
// Children promise has been resolved
a = children.length;
});
Upvotes: 1