Stephen
Stephen

Reputation: 717

Knockout js gathering data from other view models

I have an app that uses several different view models (this is the first time I have built an app with knockout js).

Basically what I'm doing is a wizard and each page is a knockout view model, at the end I'd like to take all the json from all the view models and submit it with a final button.

What would be the best way of doing this?

Upvotes: 1

Views: 78

Answers (2)

Ilya
Ilya

Reputation: 29693

window.firstViewModel = new function()
{  
   var self = this;
   self.firstProperty = ko.observable();
   //  
}  

window.secondViewModel = new function()
{  
   var self = this;
   self.secondProperty = ko.observable();
   //  
}  

var submit = function()  
{
   var firstProperty = firstViewModel.firstProperty(); // access to firstViewModel 
   var secondProperty = secondViewModel.secondProperty(); // access to secondViewModel
   //...  
}

Upvotes: 1

Srikanth Venugopalan
Srikanth Venugopalan

Reputation: 9049

I would recommend going towards a Single Page Application.

Essentially, you would have a single view model with sections for each step in the wizard, and on submit, you have all the data you need.

The visibility/aesthetics can be controlled via css and intelligent binding.

RP Niemeyer has a good demo, and also talks about it in this answer.

Upvotes: 1

Related Questions