gpt4 book ai didi

javascript - 添加新的 Observable 到由 ko.mapping.fromJS 映射的 ObservableArray

转载 作者:行者123 更新时间:2023-12-03 07:43:13 25 4
gpt4 key购买 nike

我正在尝试向 ObservableArray 添加一个新的 Observable,该 ObservableArray 最初已使用 KO 的映射插件进行了映射。但我没能做到这一点。 Firebug 告诉我“TypeError:totalChf 不是一个函数”。查看添加的 Observable,我注意到计算函数尚未创建。我尝试了几种方法,仍然没有成功...我错过了什么?

提前致谢

代码如下:

var vm;
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
})
}
$(function () {
vm = new viewModel();
vm.clients(ko.mapping.fromJS(data, ClientsMapping)());

var cont1 = {
'ContName': 'ContXX',
'MoneyAccounts': [
{ Currency: "XXX", Amount: 1000 },
]
};
var cont2 = {
'ContName': 'ContYY',
'MoneyAccounts': [
{ Currency: "YYY", Amount: 1000 },
]
};
var cont3 = {
'ContName': 'ContZZ',
'MoneyAccounts': [
{ Currency: "ZZZ", Amount: 1000 },
]
};
var cont4 = {
'ContName': 'ContWW',
'MoneyAccounts': [
{ Currency: "WWW", Amount: 1000 },
]
};

vm.clients()[0].Containers.push(ko.mapping.fromJS(cont1, ContainersMapping));//Attempt1
vm.clients()[0].Containers.push(ko.mapping.fromJS(cont2));//Attempt2
vm.clients()[0].Containers.push(new ContainerModel(cont3));//Attempt3
vm.clients()[0].Containers.push(ko.mapping.fromJS([cont4], ContainersMapping)()[0]);//Attempt4
//...still no success.
})

最佳答案

您通常希望保持映射彼此独立。一个好方法是在每个类中定义并执行映射:

var ClientModel = function(data) {
var mapping = {
'Containers': {
create: function(options) {
var container = new ContainerModel(options.data)

//Some computed observables for level two here...
return container;
}
}
}
ko.mapping.fromJS(data, mapping, this);
}

一旦组织好,您就有几个选项可以将新项目添加到 observableArray:

  1. 创建新项目并将其推送到数组:

    vm.clients()[0].Containers.push(new ContainerModel(cont1))
  2. 在映射中指定关键选项:

    var mapping = {
    'Containers': {
    key: function(item) {
    return ko.unwrap(item.ContName);
    },
    create: function(options) {
    var container = new ContainerModel(options.data)

    //Some computed observables for level two here...
    return container;
    }
    }
    }

    然后使用mappedCreate添加项目:

    vm.clients()[0].Containers.mappedCreate(cont1);

JSFiddle

关于javascript - 添加新的 Observable 到由 ko.mapping.fromJS 映射的 ObservableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35320054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com