0
votes

So I spent the entire day with this and its problebly an easy issue but I would love some help

I want to use the Knockout Mapping plugin and be able to nest viewmodels.

As I undestand it the ko.mappingfromJS() method returns a knockout viewmodel. But since I have several datasources for one page, Im wondering how I could get something like.

data1 = {"name": John, "age": 23}
data2 = {"name": Jakob, "age": 22}
viewmodel.dataSource1 = mappingfromJS(data1);
viewmodel.dataSource2 = mappingfromJS(data2);

and then in the HTML use the data-binding="with:dataSource1" on a div

All the different methods I've tried have resulted in different errors so I would really appreciate if someone could point me in a direction for doing this.

1
can you please post what you have tried to help you in it? - ebram khalil

1 Answers

0
votes

Figure I'd share the solution I went with

var self = this;
self.dataSource1 = ko.observable()

getData(function (data) {
    self.dataSource1(mapping.fromJS(data));
});