I've been looking for a solution but couldn't find it anywhere. I am trying to create an ko.computed which is dependant on two other observables. Whenever that observable changes, I was hoping for my computed observable to also update, but it does not. Computed function is not updating(view + variable itself) when I change any of its dependencies. Also another question which could be releated to this problem. Why is "self" variable global? Even if I remove it from AppViewModel, it is still global.
Here is short version and below you can find an JSFiddle for a full working example. When you press "Attack" your Strength should increase from 5 to 1000, which in turn should update progress bar "maxExp" value from 0/6 to some high value 0/500 or something.
function AppViewModel() {
var self = this;
player = new playerTest();
enemies = new enemiesTest();
activeEnemy = new initializeFirstEnemy();
console.log('Call me more pls');
};
var baseStatsArray = ['Strength', 'Endurance', 'Speed', 'Luck'];
var currentEnemy = ko.observable(0);
function playerTest() {
var self = this;
var value;
var growth;
this.baseStats = {};
for (var i = 0; i < baseStatsArray.length; i++) {
var attr = baseStatsArray[i];
this.baseStats[attr] = {};
this.baseStats[attr]['value'] = ko.observable(5);
this.baseStats[attr]['growth'] = ko.observable(2);
this.baseStats[attr]['minExp'] = ko.observable(0);
this.baseStats[attr]['maxExp'] = ko.computed(function () {
return Math.floor((10 + (self.baseStats[attr]['value']() / 2)) / self.baseStats[attr]['growth']());
});
};
};