[英]ComputedObservables doesn't recalc after ko.mapping.fromJS
I'm using KnockoutJS and I have a nested ViewModel-Structure with Computed Observables on each level. 我正在使用KnockoutJS,并且在每个级别上都有一个嵌套的ViewModel-Structure与Compute Observables。 The parent levels contains totalization of it's children. 父级包含其子级的总计。 The datastructure looks like this (simplyfied): 数据结构如下所示(简化):
vm.clientsRawData = [
{
ClientName: "Thomas",
MoneyAccounts: [
{
Currency: "USD",
Amount: "1000"
}
{
Currency: "EUR",
Amount: "2000"
}
]
},
{
ClientName: "Ann",
MoneyAccounts: [
{
Currency: "CHF",
Amount: "4000"
}
{
Currency: "EUR",
Amount: "1500"
}
]
}
]
After initializing the ViewModel (using ko.mapping.fromJS) I saw that the ComputedObservables on the "Top-Level (for instance, vm.AllUsd(), containing the Sum of each clients Usd MoneyAccounts)" didn't refresh. 初始化ViewModel(使用ko.mapping.fromJS)后,我看到“顶层(例如vm.AllUsd(),其中包含每个客户的Usd MoneyAccounts的总和)”上的ComputedObservables没有刷新。 The Sum is still 0. What do i have to do to Calculate this total amounts within the Mapping of the JS-Structure? 总和仍然为0。在JS结构的映射中,我该怎么做才能计算出总金额?
I've already tried to achieve this with dummy-observables, but this extremely slows down the Loading-Process (Browser overwhelmed, hanging up) 我已经尝试过使用虚拟可观察对象来实现此目的,但这极大地减慢了加载过程(浏览器不堪重负,挂断了电话)
Thanks in advance 提前致谢
UPDATED: For instance, this example. 更新:例如,此示例。 I found out, that the Client-Count is 0 after inizialization. 我发现,初始化后,客户计数为0。 But why? 但为什么?
var ClientsMapping = {
create: function (options) {
var client = ko.mapping.fromJS(options.data, ContainersMapping)
//Some computed observables for level one here...
return client;
}
}
var ContainersMapping = {
'Containers': {
create: function (options) {
var container = ko.mapping.fromJS(options.data, MoneyAccountsMapping)
container.totalChf = ko.computed(function () {
var total = 0;
$.each(container.MoneyAccounts(), function () {
if (this.Currency() == "CHF") {
total += this.Amount();
}
})
return total;
})
//Some computed observables for level two here...
return container;
}
}
}
var MoneyAccountsMapping = {
'MoneyAccounts': {
create: function (options) {
var macc = new MoneyAccountModel(options.data)
//Some computed observables for level three here...
return macc;
}
}
}
var ClientModel = function (data) {
ko.mapping.fromJS(data, {}, this);
}
var ContainerModel = function (data) {
ko.mapping.fromJS(data, {}, this);
}
var MoneyAccountModel = function (data) {
ko.mapping.fromJS(data, {}, this);
}
var data = [
{
'Clients': 'Thomas',
'Containers': [
{
'ContName': 'Cont01',
'MoneyAccounts': [
{ Currency: "CHF", Amount: 1000 },
]
}
]
},
{
'Clients': 'Ann',
'Containers': [
{
'ContName': 'Cont01',
'MoneyAccounts': [
{ Currency: 'CHF', Amount: 1000 },
{ Currency: 'EUR', Amount: 500 }
]
}
]
}
]
function viewModel() {
var self = this;
self.clients = ko.observableArray()
self.clientsCount = ko.computed(function () {
return self.clients().length
})
}
var vm;
$(function () {
vm = new viewModel();
vm.clients = ko.mapping.fromJS(data, ClientsMapping);
})
Ok, got it. 好的,我知道了。 I had to call the clients observableArray like this: 我不得不这样称呼客户observableArray:
vm.clients(ko.mapping.fromJS(data, ClientsMapping)())
Many thanks Eric for pointing me to the right direction. 非常感谢Eric为我指出正确的方向。 You saved me! 你救了我! :-) :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.