简体   繁体   English

如何将Knockout js模型绑定到向导样式UI

[英]How to bind a Knockout js model to a wizard style UI

I am using Knockout js. 我正在使用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 如果用户在步骤1中选择“是”,则我显示步骤2a
  • If the user selects 'No' on step 1 then I display step 2b (ie. a different dialog form) 如果用户在步骤1中选择“否”,则显示步骤2b(即,不同的对话框形式)

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). 我的问题是我在启动时将所有可能的向导UI步骤绑定到视图模型,即使某些步骤永远不会显示并且某些屏幕上的绑定将无效(例如,步骤5可能绑定到viewModel.theObject.PropertyA。 PropertyB.PropertyC()但在步骤1中PropertyB仍然为null。

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! 一个更好的方法可能是在显示它们时绑定到UI步骤,但我的问题就在那里我不知道一旦步骤完成就“取消绑定”模型的好方法所以我可能最终得到步骤绑定到原始列表中的多个对象!

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". 我认为这样做的一个好方法是让您的视图模型成为一系列步骤,并将您的UI绑定到“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/ 以下是该想法的粗略示例: 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. 如果步骤在observableArray中,那么您甚至可以动态添加步骤。 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. 也许您有一个所有可能步骤的列表,然后有一个“activeSteps”数组,表示根据用户的选择当前有效的步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM