Updates on an knockout.js observableArray not showing in GUI

This was a really simple problem but is was pretty annoying for a while. I was working with an observableArray in knockout.js and wanted to update a field in one of the items like this:


var ExperienceViewModel = {
  items: ko.observableArray([])
  // adding items to array...
}

function updateViewModel(data)
{
  for (var i = 0; i < viewModel.items().length; i++) {
    if (viewModel.items()[i].Id == data.Id) {
      viewModel.items()[i].Description = data.Description;
    }
  }
}

By doing changes to the viewModel items this way the user interface (webpage) wasn't updated. Strange!? To get it working I had to remove the item from the array and then add it again like this:


function updateViewModel(data)
{
  for (var i = 0; i < viewModel.items().length; i++) {
    if (viewModel.items()[i].Id == data.Id) {
      viewModel.items.splice(i, 1);
      viewModel.items.push(data);
    }
  }
}

I am not a master of knockout.js, however this solution solved my problem.

Kommentera

E-postadressen publiceras inte. Obligatoriska fält är märkta *