knockoutjs '购物车编辑器'示例问题
我试图了解如何观察和映射变量。 jsfiddle 在这里 http://jsfiddle.net/rniemeyer/adNuR/。下面 2 行 javascript 似乎设置了用于观察的变量:
this.category = ko.observable();
this.product = ko.observable();
那么上面 2 行是如何实现这些技巧的呢?它如何知道如何将类别和产品映射到sampleProductCategories[]数组中?
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
小提琴加载一个资源,其中包含
sampleProductCategories
数组中的所有选项:http://knockoutjs.com/examples/resources/sampleProductCategories.js每个
cartLine
对象都有category
和product
observables 来保存当前的选择。例如,以下是单个 cartLine 的绑定之一:
这表示下拉列表中的选项应来自
sampleProductCategories
。框中的值应为name
属性,并且在进行选择时,应将其放入 cartLine 的category
中。因此,当做出选择时,
category
现在等于sampleProductCategories
数组中的相应对象。类似于:现在,第二个下拉列表绑定如下所示:
如果选择了类别,则会显示此绑定。这些选项来自所选类别的
products
数组。当选择一个值时,product
observable 将使用数组中的适当对象填充。就像:所以,下拉菜单一起提供嵌套选项。
The fiddle loads a resource that contains all of the choices that live in the
sampleProductCategories
array: http://knockoutjs.com/examples/resources/sampleProductCategories.jsEach
cartLine
object hascategory
andproduct
observables 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 thename
property and when a selection is made, it should be put into thecategory
of the cartLine.So, when a selection is made,
category
now equals the appropriate object from thesampleProductCategories
array. Something like:Now, the second dropdown binding looks like:
This binding shows up if a category has been selected. The options come from the
products
array of the selected category. When a value is selected, then theproduct
observable will be populated with the appropriate object from the array. Like:So, the dropdowns work together to provide nested options.