2016-10-17 1 views
0

DB에 "플레이어"를 나타내는 모델 개체가 있습니다. 그것의 구현 거기에 내 애플 리케이션에서 다른 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}`); 
    } 
} 

비동기 키워드는 행동에 영향을 미칠 수 있습니다. 여전히 작동하지 않으면 이벤트 수집기를 사용하여 변경 사항을 브로드 캐스팅 할 수 있습니다.