TrevTheDev
TrevTheDev

Reputation: 2737

Best way to access models children in ember

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

Answers (1)

claptimes
claptimes

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

Related Questions