2016-11-30 22 views
0

我試圖設置觀測上時奧裏利亞對象屬性的變化。我之前沒有使用過observables,所以請幫助我。基於文檔,這是我認爲會工作,但我懷疑點是拋出函數名稱或可觀察。Aurelia路上觀察到與對象屬性

export class EventEdit { 
    record = { 
    ev_date_start, 
    ev_date_end, 
    ev_description 
    }; 
    @observable record.ev_date_start; 

    record.ev_date_startChanged(newValue, oldValue) { 
    console.log("ev_date_start changed from " + oldValue + " to " + newValue); 
    } 
} 

當我更改ev_date_start的值時沒有任何反應。

回答

4

這是更好,當你需要一個複雜的對象定義一個類。

import { observable } from 'aurelia-framework'; 

export class EventEdit { 
    constructor() { 
    this.model = new EventModel(); 

    setTimeout(() => { 
     this.model.ev_date_start = "test"; 
    }, 2000); 
    } 
} 

export class EventModel { 
    @observable ev_date_start; 
    ev_date_end; 
    ev_description; 

    ev_date_startChanged(newValue, oldValue) { 
     console.log("ev_date_start changed from " + oldValue + " to " + newValue); 
    } 
} 

另一種解決方案是使用BindingEngine:

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

@inject(BindingEngine) 
export class EventEdit { 
    record = { 
    ev_date_start, 
    ev_date_end, 
    ev_description 
    }; 

    constructor(bindingEngine) { 
    this.bindingEngine = bindingEngine; 
    } 

    attached() { 
    this.subs = this.bindingEngine 
     .propertyObserver(this.record, 'ev_date_start') 
     .subscribe(this.ev_date_startChanged); 
    } 

    detached() { 
    this.subs.dispose(); 
    } 

    ev_date_startChanged(newValue, oldValue) { 
    console.log("ev_date_start changed from " + oldValue + " to " + newValue); 
    } 
} 
+0

謝謝!我希望避免這種情況,因爲我沒有包含很多屬性來保持示例簡單。我會觀察下一兩天是否有其他解決方案,如果沒有其他解決方案,我會將您的答案標記爲已接受的答案。感謝您花時間,法比奧! – LStarky

+0

還有另外一個解決方案,我會後 –

+0

請參閱更新的答案!我會看看是否有方法使用@observable –

相關問題