我试图强制更新从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