Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Aurelia中阵列特性的观测_Aurelia - Fatal编程技术网

Aurelia中阵列特性的观测

Aurelia中阵列特性的观测,aurelia,Aurelia,我的类中有一个属性: class Control { @bindable households; get people() { return households .map(household => househould.people) .reduce((g1, g2) => g1.concat(g2), []); } } 我使用它来计算所有家庭中所有人[]的集合,然后在此处呈现: <u

我的类中有一个属性:

class Control {
    @bindable households;

    get people() {
        return households
            .map(household => househould.people)
            .reduce((g1, g2) => g1.concat(g2), []);
    }
}
我使用它来计算所有家庭中所有
人[]
的集合,然后在此处呈现:

<ul>
    <li repeat.for="person of people">
        ${person.firstName} ${person.lastName} - ${person.phone}
    </li>
</ul>
  • ${person.firstName}${person.lastName}-${person.phone}

每当有人加入家庭,或者如果计算集合中的任何元素的任何渲染属性,
firstName
lastName
phone
,我都需要更新列表。我怎么能在奥雷利亚做到这一点?如果我使用
@computedFrom()
它将检测不到数组元素的更改,并且由于所有家庭中的人员列表都是动态的,我不能只为每个元素创建一个观察者,而不创建一个系统来管理何时应该订阅/取消订阅观察者。

没错,因为我即将放弃使用谷歌搜索解决方案。这段代码最终为我工作:

<ul>
    <li repeat.for="person of people">
        <!-- May work without this rendering method,
            this is just closer to what my actual code is doing. -->
        ${renderPersonInfo(person) & signal: 'example-signal'}
    </li>
</ul>

就在我即将放弃谷歌搜索解决方案的时候。这段代码最终为我工作:

<ul>
    <li repeat.for="person of people">
        <!-- May work without this rendering method,
            this is just closer to what my actual code is doing. -->
        ${renderPersonInfo(person) & signal: 'example-signal'}
    </li>
</ul>
使用脏检查 禁用
@computedFrom()
,您将实现所需的行为

export class App {
  @bindable households;
  get people() {
    const households = this.households || []; // Make sure househoulds is defined.
    return households.reduce((people, household) => people.concat(household.people), []);
  }
}

使用脏检查 禁用
@computedFrom()
,您将实现所需的行为

export class App {
  @bindable households;
  get people() {
    const households = this.households || []; // Make sure househoulds is defined.
    return households.reduce((people, household) => people.concat(household.people), []);
  }
}

您必须尽可能避免
脏检
,信号是您的场景的完美选择。请记住,如果您想在数组上使用
computedFrom
,您可以通过查看其
length
属性来实现,例如,而不是
dirtyChecking
,类似以下
@computedFrom(“myArray.length”)
的内容,您必须尽可能避免
脏检查
,信号是您场景的完美选择。请记住,如果您想在数组上使用
computedFrom
,您可以通过查看其
length
属性来实现,例如,而不是
dirtyChecking
,如果您只使用getter,而不使用
computedFrom(“myArray.length”)
,类似于下面的
@computedFrom(“myArray.length”)
,如果没有信号,你也会得到同样的效果,因为aurelia会默认使用
dirty
策略,每秒重新计算getter 5次。另外:别忘了在分离上处理你的间隔。脏检查不是罪恶:-)这是唯一需要的技巧。嗯,我已经用这个做了测试,但它对我不起作用。切换到一个简单的方法和行为是完全不同的。如果您只使用一个getter,而不使用
computedFrom
,也不使用信号-您应该会得到相同的效果-因为aurelia将默认使用
dirty
策略,每秒重新计算5次getter。另外:别忘了在分离上处理你的间隔。脏检查不是罪恶:-)这是唯一需要的技巧。嗯,我已经用这个做了测试,但它对我不起作用。切换到一个简单的方法和行为是完全不同的。