Fiddle here
I have an array of objects that I want to map directly into a collection on my viewmodel, but I want to customise the mapping. The code doesn’t work though unless there is only one item in the data.UserReports collection.
I can do it if I add another layer into the object graph – so for instance instead of mapping data.UserReports to model.UserReports – if I had a property on the model called Foo – I could map data to foo and then access the collection via model.Foo.UserReports, but this is undesirable and messy.
I also reckon I can do it if i iterate through the data.UserReports array, and push each UserReport in turn onto the model’s collection – but again this seems clumsy.
So what’s wrong with the fiddle?
The code in that fiddle is not working at all, at least not for me. But however, here is how you would normally use the mapping plugin with an existing view model instance:
http://jsfiddle.net/Quu4f/27/
Update: You also need to / should specify to which key in the data object the mapping options should be applied:
It is however a bit weird that you get an error when you have two items with the same ID in the data set. It seems as if the “create” callback is not executed for the duplicate, but a normal observable is still created.
http://jsfiddle.net/Quu4f/29/