Search code examples
knockout.jsknockout-mapping-plugin

Initializing viewmodel with data in knockout


I am trying to set my view model with below initial data.

var initialVMData = [{"title":"Mrs","otherTitle":null,"forename":"Tom","surname":"Sam"},{"title":"Miss","otherTitle":null,"forename":"Sarah","surname":"Miller"}]

Here is my jsFiddle:

I am struggling to initialize my view model so that on first page load there are two records already populated with the in above JSON (initialVMData). I have tried using mapping plugin but no success yet. What is the simplest way to achieve this?

Thanks


Solution

  • Try this one. JSFiddle

    First you need to provide an option to initialize your JointHolder objects with some data. Then you need to map your jointholders array with JSON data. I've used $.map function for that. And that's it.