2016-02-08 170 views
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); 

回答

0

我只是理解了它自己,而這個物體玩耍。我希望這能幫助陷入同樣問題的人。

下面的代碼:

var mapping1 = { 
    create: function (options) { 
    var levelOneItems = ko.mapping.fromJS(options.data, mapping2) 
    //Some computed observables for level one here... 
    return levelOneItems; 
    } 
} 
var mapping2 = { 
    'levelTwoItemList': { 
    create: function (options) { 
     var levelTwoItems = ko.mapping.fromJS(options.data, mapping3) 
     //Some computed observables for level two here... 
     return levelTwoItems; 
    } 
    } 
} 
var mapping3 = { 
    '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, mapping1)