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

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

问题描述

我正在尝试获得员工的汇总列表。每个组织 hasMany 员工的模型。

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

当我在模板上循环访问这些时,正在加载员工,我可以看到api返回数据(Im使用 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/对象模型/计算属性和聚合数据/

当您尝试推送这个属性时,您的员工是一个异步属性将为空时尚。您需要将履行的员工提高到更高级别(如下所示):

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数据:如何在不同集合的集合中获取模型的聚合列表(@each not working)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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