knockoutjs : valueHasMutated

Explicitly Triggering the Evaluation of Observables with ValueHasMutated

In certain scenarios, you will want to be able to notify the subscribers with the latest value of an observable, You can achieve this by using the valueHasMutated function for any observable.
valueHasMutated is what is used internally to notify when the value has actually changed. However, it can be used to trigger subscribers with the same value.


<button data-bind="click: handleClick">Click me</button>


var viewmodel = function () {
    self.projectsVisible = ko.observable(false);
    self.handleClick = function () {
        self.projectsVisible.valueHasMutated();
    };
    self.projectsVisible.subscribe(function (newValue) { alert(newValue); });
}

ko.applyBindings(viewmodel);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s