Merging mapped data
There are times you may want to pull data into your ViewModel from multiple sources. When doing this you can create a map for each source. If the source variables have the same name they will overwrite the existing variables. As long as the base variable is a different name it will merge them into the ViewModel. Do this as follows:
myViewModel = ko.mapping.fromJS(firstData, firstMap); ko.mapping.fromJS(nextData, nextMap, myViewModel);
What you get is a combination of the firstData
JavaScript structure, mapped with firstMap
, combined with the nextData
JavaScript structure, and with the nextMap
mapping. If there are any duplicate base structures in nextData
, they will override the same structures in the existing firstData
JavaScript structure.