【发布时间】:2015-08-17 10:03:56
【问题描述】:
这是我的代码:
self.convertedPrice = ko.computed(function () {
console.debug('Calculating convertedPrice');
if (self.ProductListPrice() != null && self.multiplicationFactor() != null) {
return self.ProductListPrice() * self.multiplicationFactor();
}
return 0;
}).extend({notify:'always'});
self.convertedPrice.subscribe(function (newVal) {
console.debug('convertedPrice subscription fired.');
self.discountedPrice(parseFloat(newVal).toFixed(2));
});
当self.ProductListPrice 更新时,self.convertedPrice 正确更新并写入第一个调试,但不会触发订阅,因此第二个调试语句永远不会写入,self.discountedPrice 也不会更新。
我现在已经通过将订阅的内容移动到计算代码中来解决这个问题,但我想了解为什么原始订阅不起作用。如果我手动更改了self.ProductListPrice 或self.multiplicationFactor,订阅就会触发,但是当它们因我的其余代码和用户输入而更改时,订阅不会触发。
任何想法我做错了什么?
【问题讨论】:
-
如果你想让订阅在计算计算时触发,只需稍微柚木计算它。在此处使用
deferEvaluation:true示例jsfiddle.net/LkqTU/26144 -
我已经尝试过推迟评估,同时尝试自己追踪这一点,但这没有任何区别。不过感谢您的想法。
标签: knockout.js