我試圖強制更新從viewmodel到UI的計算屬性更新。不過,我只是得到初始值,沒有任何進一步的值。突變屬性失敗,因爲它不是一個函數。觸發器計算的屬性更新
網站
<script src="../../libraries/knockout-3.4.1.js"></script>
<input id="nameInput" data-bind="value: processName" type="text"/>
<input id="nodetotalInput" data-bind="value: processNodeCount" type="text"/>
視圖模型
export class ProcessViewModel {
public processName: KnockoutObservable<string>;
public processNodeCount: KnockoutObservable<number>;
constructor() {
try {
this.testService = new Test.TestService();
this.setBindings();
}
catch (e) {
console.log(e);
}
}
public setBindings(): void {
this.processName = ko.computed<string>(
function() { processViewModel.isLoaded() ? processViewModel.testService.flowModel.process.name : ""; }
);
this.processNodeCount = ko.computed<number>(
function() { processViewModel.isLoaded() ? processViewModel.testModel.nodeCount() : 0; }
);
}
public isLoaded(): boolean {
return this.testService.isLoaded();
}
public refreshProcessDetails() {
try {
let message: string = "IsLoaded" + this.isLoaded();
console.log(message);
/** attempts at triggering an update */
this.processName();
this.processName.valueHasMutated(); // fails because it's not a function
this.processNodeCount();
}
catch (e) {
console.log(e);
}
}
}
綁定
declare var processViewModel: Process.ProcessViewModel;
window.onload =() => {
processViewModel = new Process.ProcessViewModel();
processViewModel.setBindings();
ko.applyBindings(processViewModel);
}
我打電話從this.refreshProcessDetails this.processName.valueHasMutated()()時,遇到下列錯誤;
棧:「類型錯誤:this.processName.valueHasMutated不在
你的TestService沒有提供任何你可以訂閱的事件嗎?傾聽「負載」事件將更適合淘汰賽的完全事件驅動方法。像'refreshProcessDetails'一樣創建輪詢函數將導致非理想的結果。 – Tomalak