Ember-data:如何在不同集合的集合中获取模型的汇总列表(@each 不工作) [英] Ember-data: How do I get an aggregated list of models in a collection on a different collection (@each not working)

查看:25
本文介绍了Ember-data:如何在不同集合的集合中获取模型的汇总列表(@each 不工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 employees 的汇总列表.每个organization 都是一个hasMany employees 的模型.

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')

当我在模板上循环遍历这些时,正在加载 employees,我可以看到 api 返回数据(我使用的是 async: true),但是employees 的返回值仍然是一个空数组.

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.

推荐答案

Ember 不支持多级深度的依赖属性.

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.[]')
});

使用 loadedEmployees

employees: function(){

  var employees =[];

  this.get('organizations').forEach(function(organization){
    employees.pushObjects(organization.get('loadedEmployees'));
  });

  return employees;

}.property('organizations.@each.loadedEmployees')

这篇关于Ember-data:如何在不同集合的集合中获取模型的汇总列表(@each 不工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆