具有过滤值转换器的Aurelia阵列观察器

时间:2015-09-07 00:46:31

标签: javascript aurelia aurelia-binding

与此issue类似,我正在尝试设置一个Array Observer来观察字符串数组(在过滤器ValueConverter中使用),然后在数组更改时触发重新评估。

以下是我目前的情况。它似乎工作是在检查/取消选中selectedFeatures时,调用观察者回调。但是,在初始加载后永远不会调用ValueConverter。

阵列观察者:

import {inject, ObserverLocator} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';

@inject(HttpClient, ObserverLocator)
export class Home{
    flagPropertyName = false;
    selectedFeatures = [];

    constructor(http, observerLocator){
        var subscription = observerLocator
            .getArrayObserver(this.selectedFeatures)
            .subscribe(this.onFeaturesChanged);
    }

    <activate omitted>

    onFeaturesChanged(mutations) {
        this.flagPropertyName = !this.flagPropertyName;
    }
}

export class FilterPlaceValueConverter {
    toView(places, expected){
        console.log(JSON.stringify(expected));
        <code omitted>
}

在视图中:

绑定selectedFeatures:

<label repeat.for="feature of features">
    <input type="checkbox" checked.bind="$parent.selectedFeatures" value.bind="feature.name" />
    ${feature.name}
</label>

绑定ValueConverter:

<div repeat.for="place of places | filterPlace:selectedFeatures:flagPropertyName">

我粗略的理解是,在onFeaturesChanged回调中更新flagPropertyName会强制repeat.for在任何selectedFeatures数组被修改时调用ValueConverter。

1 个答案:

答案 0 :(得分:3)

这解决了它:

constructor(http, observerLocator){
    observerLocator
        .getArrayObserver(this.selectedFeatures)
        .subscribe(() => {
            this.testFlag = !this.testFlag;
        });