I have a bar which displays a varibale number of values in sub-bars. The total width of the bar is fixed. Whenever a value changes, I need to recompute the width of all the bars as proportions of the new sum of all values.
The sub-bars are templated based on a knockout.js observable array:
<div data-bind="foreach: subBars">
<div data-bind="style: { width: (width() + 'px'), backgroundColor: color }"></div>
</div>
The Array itself passes an initial width and a value into the subBars:
this.subBars = ko.observableArray ([
new subBar(initialWidth01, color01);
new subBar(initialWidth02, color02);
]);
Whenever a value for a subBar is changed, the total sum of all values is calculated as
this.subBarsTotal = ok.computed... (this works)
within the view model.
The subBar function, outside of the view model, is:
function subBar (initialWidth, color) {
var self = this;
self.initialWidth = initialWidth;
self.width = ok.computed(function(){
var adjustedWidth = self.initialWidth() / subBarsTotal() * widthOfBar;
return adjustedWidth;
}
self.color = color;
}
However much I try, I haven't found a way to get at the value of subBarsTotal.
What am I not seeing here?
(edit: typo) (edit: whole code) (edit: back to basics - whole code not