0

我浏览了很多淘汰赛文章,但我未能将 json 对象下方映射到淘汰赛视图模型

{
   "VehicleModels":[
      {
         "Name":"Model 1",
         "Model":{
            "MakeName":"Ford"
         },
         "Styles":[
            {
               "StockImage":"http://google.com"
            }
         ]
      },
      {
         "Name":"Model 2",
         "Model":{
            "MakeName":"Ford"
         },
         "Styles":[
            {
               "StockImage":"http://bing.com"
            }
         ]
      }
   ]
}

这是我的 jsfiddle 代码jsfiddle 链接

4

1 回答 1

1

好的,我已经更新了小提琴:

本质上:

var mapping = {
    'Styles': {
        create: function (options) { // I fiddled with this, play around with it
            var self = options.data;
                self.stockimage = ko.observable();
                return self;
        }
    }
};

var data = {"VehicleModels":[{"Name":"Model 1","Model":{"MakeName":"Ford"},"Styles":[{"StockImage":"http://google.com"}]},{"Name":"Model 2","Model":{"MakeName":"Ford"},"Styles":[{"StockImage":"http://bing.com"}]}]};

var viewModel = ko.mapping.fromJS(data,mapping); // Here you did not need to put this as the 3rd parameter

ko.applyBindings(viewModel); // you need to apply the bindings at some point

最后,您尝试为跨度指定一个值:

<span data-bind='value: Name' />

跨度没有值我认为你想要文本:

<span data-bind='text: Name' />
于 2014-05-29T20:31:41.847 回答