Force observable to refresh

<div data-bind=”text: curDate”></div>
<input type=”button” data-bind=”click: recalcCurDate” value=”Mutated”></input>

In order to refresh current date, we need a dummy observable and then call notifysubscribers() on dummy wherver required in order to force refresh on the curDate:-

(function() {

var vm = function() {
var $this = this;

$this.dummy = ko.observable();

$this.curDate = ko.computed(function() {
$this.dummy();
return new Date();
});

$this.recalcCurDate = function() {
$this.dummy.notifySubscribers();
};
};

ko.applyBindings(new vm());

}());

fiddle

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