2016-10-17 4 views
0

DB内に "players"を表すモデルオブジェクトがあります。実装では、私のアプリケーション内の別のVMからバインドしたいプレイヤーの配列があります。例えば:変更は、(アプリで他の部分から)プレーヤアレイに構成されている場合Aureliaのプロパティとコレクションオブザーバがプロパティを生成しません変更イベント

import {Players} from './models/players'; 
import {inject, BindingEngine} from 'aurelia-framework'; 

@inject(Players,BindingEngine) 
export class App { 

    constructor(playersProvider,bindingEngine) { 
    this._playersProvider = playersProvider; 
    this._bindingEngine = bindingEngine; 
    this._subscription = this._bindingEngine.propertyObserver(this,this._playersCount) 
     .subscribe(this.objectValueChanged); 
    } 

    async activate() { 
    await this._playersProvider.initialize(); 
    this._playersCount = this._playersProvider.players.length; 
    } 

    objectValueChanged(newVal,oldVal) { 
    console.log("new : " + newVal + ", old val : " + oldVal); 
    } 

    deactivate() { 
    this._subscription.dispose(); 
    } 
} 

残念ながら、変更が_playersCountプロパティに反映されません。例えば - このプロパティにバインドされたUIラベルはリフレッシュされず、objectValueChangedは決して呼び出されません。

Uは、同じアレイ上のcollectionObserverを持つ異なるVMで同じ問題を抱えています。

助けてくださいか?

+0

構文は 'this._bindingEngine.propertyObserver(this、 '_playersCount')' –

答えて

1

_playersCountをサブスクライブする前にコンストラクタで宣言しようとしましたか?

またsynthaxが正しいいないようです、それはthis articleに応じて次のようになります。

import {BindingEngine, inject} from 'aurelia-framework'; 

@inject(BindingEngine) 
class MyClass { 
    constructor(bindingEngine) { 
    this.bindingEngine = bindingEngine; 
    this.observeMe = 'myvalue'; // the property is first initialized in the constructor 

    let subscription = this.bindingEngine 
     .propertyObserver(this, 'observeMe') // <= you wrote this._bindingEngine.propertyObserver(this,this.observeMe) 
     .subscribe(this.objectValueChanged); 

    // Dispose of observer when you are done via: subscription.dispose(); 
    } 

    objectValueChanged(newValue, oldValue) { 
    console.log(`observeMe value changed from: ${oldValue} to:${newValue}`); 
    } 
} 

非同期キーワードが行動に影響を与える可能性があります。 それでも動作しない場合は、イベントアグリゲータを使用して変更をブロードキャストできます。

関連する問題