5
我是新来的knockout.js,我不知道如何对我的对象进行多重映射。 这是我的数据从服务器:knockout.js嵌套对象中的多重映射
var persons = {
'value': [
{
"id": "1",
"civility": "Mr.",
"firstname": "john",
"lastname": "doe",
"phone": "00 00 00 00 00",
"email": "[email protected]",
"contract": [
{
"id": "1",
"gamme": "Gamme 1",
"formula": "Formula 1"
},
{
"id": "2",
"gamme": "Gamme 2",
"formula": "Formula 2"
}
]
}
]
}
我做整个对象上的第一映射和ko.computed一些DATAS:
var person_mapping = {
'value': {
create: function (options) {
if (options.data != null) {
return new myPersonModel(options.data);
}
}
}
}
var myPersonModel = function (data) {
ko.mapping.fromJS(data, {}, this);
this.fullName = ko.computed(function() {
return this.civility() + ' ' + this.lastname() + ' ' + this.firstname();
}, this);
}
执行此操作后,我得到了我想要的第部分:
self.dataModel = ko.mapping.fromJS(persons, person_mapping);
但现在,我想做同样的事情与合同阵列内的人反对,也就是应用映射,并做一些像ko.computed这个:
var contract_mapping = {
'contract': {
create: function (options) {
if (options.data != null) {
return new myContractModel(options.data);
}
}
}
}
var myContractModel = function (data) {
ko.mapping.fromJS(data, {}, this);
this.contractName = ko.computed(function() {
return this.gamme() + ' ' + this.formula();
}, this);
}
但我不知道如何申请我的第二个映射,似乎没有任何工作。
谢谢你,它是如此简单,我没有想到这一点。我试图在第一个之后做映射,而我必须在第一个中做这个。 –