Skip to content Skip to sidebar Skip to footer

Using Knockout To Update The View From Json Via Click Event

I'm attempting to update the view model every time an event fires (e.g. button click) using Knockout. When the red button is clicked, it should read 'red flower'. When blue is cli

Solution 1:

You can try that:

$('.red').click(function() {
  var viewModel = ko.mapping.fromJS(data.colors[0]);
  ko.cleanNode(document);
  ko.applyBindings(viewModel);
});

$('.blue').click(function() {
  var viewModel = ko.mapping.fromJS(data.colors[1]);
  ko.cleanNode(document);
  ko.applyBindings(viewModel);
});

It works but it's not the recommended way to use knockout.

Post a Comment for "Using Knockout To Update The View From Json Via Click Event"