findAll具有父级定义的属性

问题描述 投票:1回答:1

2个组成部分,1个父母1个孩子。父级是带按钮的列表。用户单击该按钮,子编辑器变为可见,变量listType从父级传递给子级。

我希望一个属性基于listType属性在子节点中执行findAll,例如

listType:null,
records: this.get('store').findAll(this.get('listType'));

问题是当子编辑器第一个inits listType未定义时,需要花费一些时间从父进程传递数据。我应该如何基于listType计算记录,以便在从父列表中选择不同的listType时可以动态更改记录,而在init上未定义listType属性时不会崩溃?

这可能看起来有点奇怪,但我有20种不同的listTypes,能够以这种方式计算记录将使我不必创建额外的40个文件来保存和删除所需的模型类型。

ember.js dynamic parent record findall
1个回答
1
投票

看起来您的子组件中需要一个computed property,它能够对缺少的listType做出反应。

import Component from '@ember/component';
import { computed } from '@ember/object';
import { inject as service } from '@ember/service';

export default Component.extend({
  store: service(),
  listType: null,
  records: computed('listType', function () {
    if (!this.listType) {
      return null;
    }

    return this.store.findAll(this.listType);
  }),
});

records将返回一个承诺,您将不得不在模板中以这种方式处理它。我个人使用这种方法https://stackoverflow.com/a/40180704/796999

另一种选择可能是将records传递给子组件而不是在那里查找它们。您将以相同的方式创建计算机属性,只需将父模板更改为{{child-component records=records}}

© www.soinside.com 2019 - 2024. All rights reserved.