How to bind a Knockout js model to a wizard style UI
I am using Knockout js. I have a view model that contains an array of object开发者_运维百科s and I want to allow the user to edit one of the objects using a wizard style interface. The issue I have is the wizard will show different steps depending on what choices are made. For instance:
- If the user selects 'Yes' on step 1 then I display step 2a
- If the user selects 'No' on step 1 then I display step 2b (ie. a different dialog form)
This goes on so that the paths through the wizard are not linear.
My question is do I bind all the possible wizard UI steps to the view model at start up even though some steps will never be shown and the bindings on some screens will be invalid (eg. step 5 may bind to viewModel.theObject.PropertyA.PropertyB.PropertyC() but PropertyB is still null at step 1).
A better way may be to bind to the UI steps as they are displayed but my problem is then there I am not aware of a good way to 'unbind' the model once the step has completed so I could end up with the step bound to multiple objects from the original list!
I think that a good way to do this is to have your view model be an array of steps and bind your UI to the "selectedStep". Then, each step can dynamically choose which template that it wants to use (like in this post).
Here is a rough sample of the idea: http://jsfiddle.net/rniemeyer/SSY6n/
This way the template bindings handles generating/binding/cleaning up the dynamic content based on whatever step is selected. If the steps are in an observableArray, then you could even dynamically add steps. Maybe you have a list of all of the possible steps and then have an "activeSteps" array that represents the steps that are currently valid based on the user's choices.
精彩评论