Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 objects 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!

like image 944
Mark Robinson Avatar asked Sep 15 '11 09:09

Mark Robinson


People also ask

How do you bind a function in KnockoutJS?

The function you want to bind to the element's click event. You can reference any JavaScript function - it doesn't have to be a function on your view model. You can reference a function on any object by writing click: someObject. someFunction .

What are the types of binding supported by KnockoutJS?

Binding Values The binding value can be a single value, literal, a variable or can be a JavaScript expression.

What is two way binding in KnockoutJS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.

How do you activate a KnockoutJS model?

Activating Knockout But since the browser doesn't know what it means, you need to activate Knockout to make it take effect. To activate Knockout, add the following line to a <script> block: ko. applyBindings(myViewModel);


1 Answers

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.

like image 121
RP Niemeyer Avatar answered Sep 29 '22 17:09

RP Niemeyer