Knockout: Forcing an observable to update

Created on 20 Aug 2012  ·  3Comments  ·  Source: knockout/knockout

Hi All,

I'm facing a problem at the moment where I need to be able to force an observable to fire its update handler regardless of whether the new value is different from _latestValue or not. This is because my observable is being called as a callback rather than being triggered directly by a change event on the bound element (it's a custom autocomplete control).

The ideal solution would be a "force" flag on the observable:

myObservable("new value", /* force = */ true)

Is there any other way to do this?

Cheers, Andrew.

Most helpful comment

observables have a valueHasMutated function that you can call for this purpose.

You just need to do:

myObservable.valueHasMutated()

All 3 comments

observables have a valueHasMutated function that you can call for this purpose.

You just need to do:

myObservable.valueHasMutated()

You can also make the observable always notify when it's updated:

myObservable = ko.observable().extend({notify: 'always'});
myObservable("new value");  // will notify even if the value is already "new value"

It's probably a good idea to have both of these methods in the documentation.

valueHasMutated works like a charm! Thanks, guys :-)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcarpenterjr picture mcarpenterjr  ·  3Comments

IPWright83 picture IPWright83  ·  7Comments

aminroosta picture aminroosta  ·  5Comments

Umar-Mukhtar picture Umar-Mukhtar  ·  8Comments

Apollo3zehn picture Apollo3zehn  ·  3Comments