简体   繁体   中英

Knockout update observableArray item does not reflect in UI?

The problem when my viewModel has observableArray (self.images) and I want when one item property (mm) is changed to be reflected on UI

in the real code the images is loaded from server and mapped to object DOcImage

here is the code note that i have been trying few things based on other questions answers but no luck yet

here is fiddle http://jsfiddle.net/mosta/jt6bbeq4/21/ when click "change" it should display first item = 2, according to line in upate function

    var DocImage = function () {
              var self = this;              
              self.mm = ko.observable('1');
    self.nn = ko.observable('1');
          };
    var viewModel = function () {
              var self = this;
            self.images = ko.observableArray([DocImage]);
              self.update = function () {
                  self.images()[0].mm = '2';
              }
          };

          var vm = new viewModel(); 
    vm.images =  [{ mm: ko.observable('1') ,nn: ko.observable('2') }, {mm:ko.observable('3'), nn:ko.observable('4')}];
ko.applyBindings(vm);

Appreciate your help , thanks

When you set

m.images =  [{ mm: ko.observable('1')  ...

you actually re-write your observableArray with simple array. To update the value inside observable array you need to use another syntax (See documentation )

m.images([{ mm: ko.observable('1')  ...);

The same is true for simple observables like mm

self.images()[0].mm('2');

See fiddle

Here you go:

var viewModel = function () {
              var self = this;
            self.images = ko.observableArray([DocImage]);
              self.update = function () {
                  self.images[0].mm('2');
              }
          };

          var vm = new viewModel(); 
vm.images =  [{ mm: ko.observable('1') ,nn: ko.observable('2') }, {mm:ko.observable('3'), nn:ko.observable('4')}];
ko.applyBindings(vm);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM