Search code examples
javascriptknockout.jsknockout-mapping-pluginknockout-3.0

Knockoutjs mapping doesn't work


This is my http://jsfiddle.net/ENMGp/468/ sample code

My Json object

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

I want to access Styles array, but my viewmodel doesn't allow me to access


Solution

  • Right now your Styles properties is array, you can access it by Styles()[0].StockImage

    1. Here is the working version for object: http://jsfiddle.net/ysN4m/

    2. And here is the working version for array: http://jsfiddle.net/c6Qvq/

    And the other changes is, use <span></span> instead of <span />