weeklyAmounts
子オブジェクトで定義されたフロートの配列がありますa
。そして、親 objectb
には object の配列がありa
、すべての子 object から week1 の小計を計算し、すべての子 object から week2 の小計を計算する必要がありa
ますa
。
私の質問:
- ノックアウトのドキュメントを読むと、監視可能な配列は削除や追加などのイベントで追跡されますが、個々のオブジェクトの状態の変化は追跡されないため、私の場合、ユーザーが
week1Amount
GUI から変更したとしても、計算された値はトリガーされません。本当? week1Amount
GUI から変更した後、viewmodel で値が変更されていないことに驚きました。ここで何か見逃しましたか?- 何が必要かを考えると、最適なソリューションは何ですか?
私の動作しないコードは次のとおりです。
function a(id, name, week1Amount, week2Amount, week3Amount, week4Amount,
week5Amount, week6Amount, week7Amount, week8Amount, week9Amount, week10Amount...) {
var self = this;
self.id = id;
self.name = name;
self.week1Amount = ko.observable(week1Amount);
self.week2Amount = ko.observable(week2Amount);
self.week3Amount = ko.observable(week3Amount);
self.week4Amount = ko.observable(week4Amount);
self.week5Amount = ko.observable(week5Amount);
self.week6Amount = ko.observable(week6Amount);
self.week7Amount = ko.observable(week7Amount);
self.week8Amount = ko.observable(week8Amount);
self.week9Amount = ko.observable(week9Amount);
self.week10Amount = ko.observable(week10Amount);
self.week11Amount = ko.observable(week11Amount);
self.week12Amount = ko.observable(week12Amount);
};
function b(bid, bname, as) {
var self = this;
self.id= bid;
self.name = bname;
self.as = as;
self.week1Sum = ko.computed(function () {
var sum = 0.0;
$(self.as).each(function (index, aItem) {
sum += aItem.week1Amount();
});
return sum;
});
//rest of code is omitted for brevity.
};
var objBs = [];
$(objAs).each(function (index, aItem) {
var newA = new a(aItem.ID, aItem.Name,
aItem.WeeklyAmount[0],aItem.WeeklyAmount[1],aItem.WeeklyAmount[2],aItem.WeeklyAmount[3],
aItem.WeeklyAmount[4],aItem.WeeklyAmount[5],aItem.WeeklyAmount[6],....);
objBs.push(newA);
});
var vmB = new b(1,"someName", objBs);
ko.applyBindings(vmB, document.getElementById("showBs"));