子對象考慮這個JSON對象:擴展與淘汰賽映射
{
"BusinessModels":[
{
"Id":1,
"Name":"Business to Business",
"Businesses":[],
"ReportTypes":[
{...},
{...},
{
"Id":6,
"Name":"Risk",
"BusinessModelId":1,
"Reports":[
{
"Id": 4,
"Name": "Test",
"Value": ko.observable() // NEED TO ADD THIS PROPERTY USING MAPPING
},
{...}
]
}
],
}
]
}
我嘗試添加使用下面的代碼的價值,但它似乎並沒有工作。
var mapping = {
'BusinessModels': {
create: function(options) {
return new function() {
var self = options.data;
self.SelectedBusiness = ko.observable();
self.Businesses.unshift({ Id: 0, Name: 'All Clients' });
ko.mapping.fromJS(self, {}, this);
};
}
},
'Reports': {
create: function (options) {
return new function() {
var self = options.data;
self.Value = ko.observable();
ko.mapping.fromJS(options.data, {}, this);
};
}
}
};
var model = ko.mapping.fromJSON(raw, mapping);
ko.applyBindings(model);
我需要給子對象「報告」評爲Value
一個ko.observable
對象。我如何去做這個在ko.mapping?
我不太確定你在問什麼。如果你想用你想要的結構手動構建它,你能發佈一個「之後」對象嗎? –
我更新了我的評論。我只需要添加一個屬性到一個孩子,我的映射不起作用。 – dimiguel