由其他Ember Select填充的Ember Select不更新选择绑定

Ember Select populated by other Ember Select not updating selection binding

本文关键字:Select Ember 更新 选择 绑定 填充 其他      更新时间:2023-09-26

我是Ember.js的新手,我正试图弄清楚Select视图如何将其选择绑定到控制器。我有这个模板:

{{view Ember.Select
  contentBinding="content"
  selectionBinding="selectedCompany"
  optionLabelPath="content.name"}}

{{view Ember.Select
  contentBinding="selectedCompany.employees"
  selectionBinding="selectedEmployee"
  optionLabelPath="content.name"}} 

Employee: {{selectedEmployee.name}}
Age: {{selectedEmployee.age}}

我发现当selectedCompany发生变化时,第二个select确实会更新,但在物理上将第二个select的值更改为不同的值之前,我无法引用selectedEmployee中的nameage的值。

如何在第一次选择更改时设置selectedEmployee,以便使用它的值?

为了更清楚,我在这里有我的工作代码。

更改第二个选择框的内容实际上不会更改其选择。您必须手动操作,例如,将以下方法添加到控制器

onChangeCompany : function() {
    if (!this.get('selectedCompany.employees').contains(this.get('selectedEmployee'))) {
      this.set('selectedEmployee', this.get('selectedCompany.employees').objectAt(0));
    }
}.observes('selectedCompany')

它仍然需要一些非null和数组范围的检查,但它确实做到了——如果你选择了一名员工,然后换了公司,员工也会换。