问题
I am trying to get an aggregated list of employees
. Each organization
is a model that hasMany
employees
.
var Organization = DS.Model.extend({
name: attr('string'),
employees: hasMany('employee', { async: true })
});
On my controller, I have this property.
employees: function(){
var employees =[];
this.get('organizations').forEach(function(organization){
employees.pushObjects(organization.get('employees'));
});
return employees;
}.property('organizations.@each.employees.isFulfilled')
When I loop through these on the template, the employees
are being loaded, I can see the api returning the data (Im using async: true
), but the return value of employees
is still an empty array.
It seems like I might be listening to the wrong thing on the property. Please help.
回答1:
Ember doesn't support dependent properties multiple levels deep.
http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/
And your employees being an async property will be empty when you attempt to push in this fashion. You'd need to move the fulfilled employees a level higher (something like this):
var Organization = DS.Model.extend({
name: attr('string'),
employees: hasMany('employee', { async: true }),
loadedEmployees: function(){
return this.get('employees');
}.property('employees.[]')
});
With a computed property using loadedEmployees
employees: function(){
var employees =[];
this.get('organizations').forEach(function(organization){
employees.pushObjects(organization.get('loadedEmployees'));
});
return employees;
}.property('organizations.@each.loadedEmployees')
来源:https://stackoverflow.com/questions/24987864/ember-data-how-do-i-get-an-aggregated-list-of-models-in-a-collection-on-a-diffe