I’m trying to understand how the variables are observed and mapped. The jsfiddle is here http://jsfiddle.net/rniemeyer/adNuR/. The following 2 lines of javascripts seem to set up the variables for observation:
this.category = ko.observable();
this.product = ko.observable();
So how do the above 2 lines do the tricks? How does it know how to map the category and product into the sampleProductCategories[] array?
Thanks for the help.
The fiddle loads a resource that contains all of the choices that live in the
sampleProductCategoriesarray: http://knockoutjs.com/examples/resources/sampleProductCategories.jsEach
cartLineobject hascategoryandproductobservables to hold the current choice.For example, here is one of the bindings for the individual cartLine:
This says that the options in the dropdown should come from
sampleProductCategories. The value in the box should be thenameproperty and when a selection is made, it should be put into thecategoryof the cartLine.So, when a selection is made,
categorynow equals the appropriate object from thesampleProductCategoriesarray. Something like:Now, the second dropdown binding looks like:
This binding shows up if a category has been selected. The options come from the
productsarray of the selected category. When a value is selected, then theproductobservable will be populated with the appropriate object from the array. Like:So, the dropdowns work together to provide nested options.