0
我試圖用Knockout的映射插件映射嵌套的ViewModel(三個深度級別)。運行此代碼時,只有第一級將正確映射。我在這裏做錯了什麼?映射嵌套的KO ViewModels
在此先感謝
這裏是我的代碼:
var mapping = {
create: function (options) {
var levelOneItems = new levelOneModel(options.data)
//Some computed observables for level one here...
return levelOneItems;
},
'levelTwoItemList': {
create: function (options) {
var levelTwoItems = new levelTwoModel(options.data)
//Some computed observables for level two here...
return levelTwoItems;
},
'levelThreeItemList': {
create: function (options) {
var levelThreeItems = new levelThreeModel(options.data)
//Some computed observables for level three here...
return levelThreeItems;
}
}
}
}
var levelOneModel = function (data) {
ko.mapping.fromJS(data, {}, this);
}
var levelTwoModel = function (data) {
ko.mapping.fromJS(data, {}, this);
}
var levelThreeModel = function (data) {
ko.mapping.fromJS(data, {}, this);
}
var data = [
{
'LevelOneName': 'Apple1',
'levelTwoItemList': [
{
'LevelTwoName': 'Apple2.1',
'levelThreeItemList': [
{ 'LevelThreeItemName': 'Apple3.1' },
{ 'LevelThreeItemName': 'Apple3.2' }
]
}, {
'LevelTwoName': 'Apple2.2',
'levelThreeItemList': [
{ 'LevelThreeItemName': 'Apple3.3' },
{ 'LevelThreeItemName': 'Apple3.4' }
]
},
]
}
]
var viewModel = ko.mapping.fromJS(data, mapping);