Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing an observable to update #610

Closed
andrewchch opened this issue Aug 20, 2012 · 3 comments
Closed

Forcing an observable to update #610

andrewchch opened this issue Aug 20, 2012 · 3 comments

Comments

@andrewchch
Copy link

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.

@rniemeyer
Copy link
Member

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

You just need to do:

myObservable.valueHasMutated()

@mbest
Copy link
Member

mbest commented Aug 20, 2012

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.

@andrewchch
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants