我从 API 取回 JSON 数据,我想计算 InvestmentSummary.InvestmentDetails 数组中所有“成本”字段的总成本。我收到以下错误:
Error: Unable to parse bindings.
Message: SyntaxError: invalid object initializer;
Bindings value: totalCost
我的 viewModel 代码如下所示:
var init = { InvestmentSummary: { InvestmentDetails: [] } };
ko.mapping.fromJS(init, {}, self);
self.totalCost = ko.computed({
read: function () {
var result = 0;
ko.utils.arrayForEach(self.InvestmentSummary.InvestmentDetails, function () {
result += item.Cost();
});
return result;
}
},self);
self.createBtnClick = function () {
self.layoutView(true);
var investAPI = "some_api";
$.getJSON(investAPI, function (result) {
ko.mapping.fromJS(result, {}, self);
});
};
我的 JSON 数据如下所示:
{
InvestmentSummary: {
InvestmentDetails: [
{
Cost: 0
},
{
Cost: 1
}]
}
}
我究竟做错了什么?谢谢