Reputation: 21034
I have a simple knockout viewmodel problem, which is driving me round in circles... the siteId property binds the first time, but doesn't update on click:
Anyone see what's wrong with this?
function NewSiteAccessViewModel() {
var self = this;
self.siteId = ko.observable();
}
$(document).ready(function () {
var newSiteAccessViewModel = new NewSiteAccessViewModel();
newSiteAccessViewModel.siteId = 'none yet';
ko.applyBindings(newSiteAccessViewModel);
$(".testClick").click(function() {
newSiteAccessViewModel.siteId = "a new one";
alert(newSiteAccessViewModel.siteId);
});
});
HTML:
<h3 data-bind="text:siteId" ></h3>
So the result I get changes the value to "none yet", but doesn't subsequently update on click. The alert shows the new value...
Upvotes: 1
Views: 60
Reputation: 311865
For Knockout's view model bindings to update, you must do two things:
ko.observable()
.So in this case:
siteId
must be created in the view model as this.siteId = ko.observable();
siteId
must be initialized as newSiteAccessViewModel.siteId('none yet');
siteId
must be updated as newSiteAccessViewModel.siteId('a new one');
Upvotes: 2
Reputation: 12295
You set the value of an observable by invoking the observable function. So where you wrote:
newSiteAccessViewModel.siteId = 'none yet';
you want
newSiteAccessViewModel.siteId('none yet');
and similarly from within the click handler.
Upvotes: 2