1

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?

Stephen
  • 707
  • 3
  • 13
  • 33

2 Answers2

1

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.

Community
  • 1
  • 1
Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
  • Thank you for this answer, I may have built it this way initially but I have already built all the individual view models and formated all the html for each step. I'm looking for more of a method of having a master view model that collects the data, or holds it during the process. – Stephen Mar 25 '13 at 14:00
  • Couldn't you create a container view model that has properties for each of the step level view models? – Rich Mar 25 '13 at 14:50
1
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
   //...  
}
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • I think this is what I need. I'm going to test it out and let you know :) – Stephen Mar 25 '13 at 15:15
  • I used your code, and it's submitting a null array and deleting the data I have in my knockout fields. I pasted my code here: http://jsfiddle.net/G2y2P/1/ – Stephen Mar 25 '13 at 15:50
  • Weird, I tried the same thing and it didn't work for me. Thank you a lot! – Stephen Mar 25 '13 at 16:44