Ember-data: How do I get an aggregated list of models in a collection on a different collection (@each not working)

吃可爱长大的小学妹 提交于 2019-12-31 05:14:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!