O'Reilly logo

KnockoutJS Web Development by John Farrar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required