0
votes

I have 2 viewmodels, 1 have an observable and the second have a ko.computed that refer to the other model. I need to refresh my computed value when the first viewmodel observable is updated.

var viewModel1 = {
  value: ko.observable(1)
}
var viewModel2 = {
  result: ko.computed(function() {
    if (viewModel1.value() > 2) {
      return "xxx";
    }
    return "yyy";
  });
}

how can I subscribe with "result" to "value" in the other model?

1
If your computed has a observable within it, the computed will automatically update itself when an observable within it is updated. - Khan
Thank you for your answer. I didn't knew that and I could fix it because of this comment. - Demonia

1 Answers

0
votes

I just fixed it because adding a call to the observable in the first line (the call was inside a complex code and inside switch/case)

var viewModel2 = {
  result: ko.computed(function() {
    ko.viewModel1();
    ...
    if (viewModel1.value() > 2) {
      return "xxx";
    }
    return "yyy";
  });
}