KnockoutJS Forcing A Computed Observable to Re-Compute

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I'm new to KnockoutJS and think it's a great tool so far :)! However, I've a question regarding computed observables, and it's as follows. On the KnockoutJS page at http://knockoutjs.com/documentation/computedObservables.html, there's a sample code for formatting the price. The code is as follows.

HTML <p>Enter bid price: <input data-bind="value: formattedPrice"/></p> 

And the JS:

I've also copied the code and put a jsFiddle here: http://jsfiddle.net/wDvxw/

My issue is that the value does not update itself when you enter the same thing twice. For example:

Step 1: Enter 25.1234, it becomes $25.12 Step 2: Enter 25.1234 again. Now nothing happens.

My guess is that the value did not change and thus it doesn't reformat itself. May I know how I can go about fixing this?

Thanks!

回答1:

This is an optimization in Knockout that it does not fire the changed events if you set an observable to the same value.

So if you want to always fire the change event you need to use the valueHasMutated method to manully trigger it:

this.price(isNaN(value) ? 0 : value); // Write to underlying storage this.price.valueHasMutated(); 

In itself it won't fix your code because there is another optimization introduced for ko.computed in KO 3.0 so the computed are also not triggering the change if the calculated value remains the same and you need to use .extend({notify: 'always'}) to force the notification of the subscribers.

So the full working computed will look like this:

Demo JSFiddle.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!