Knockout component updates parent model observable, but then it gets back to old value

90 Views Asked by At

I have a counter component and pass my parent viewmodel as data param to it. When user clicks, my counter component updates an observable in my parent viewmodel (its value is changing as I subscribe to it and log the new value passed).

Simplified code of parent viewmodel:

const Day = class {
  constructor(params){
   this.counters = {
     hours: ko.observable(null),
     price: ko.observable(null)
   };

   this.counters.hours.subscribe(newValue => console.log(newValue)); //returns new value passed from component
}

   getHourValue(){
     console.log(this.counters.hours()); //returns null
   }
}

const day = new Day();

day.counters.hours(1); // Logs 1
day.getHourValue();    // Logs 1 as well...
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>

So the value seems to update, but when I call getHourValue method, it returns null again. What's the problem here? Thanks for any help!

0

There are 0 best solutions below