knockoutjs '购物车编辑器'示例问题

发布于 2024-12-11 10:01:01 字数 351 浏览 0 评论 0原文

我试图了解如何观察和映射变量。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千と千尋 2024-12-18 10:01:01

小提琴加载一个资源,其中包含 sampleProductCategories 数组中的所有选项:http://knockoutjs.com/examples/resources/sampleProductCategories.js

每个 cartLine 对象都有 categoryproduct observables 来保存当前的选择。

例如,以下是单个 cartLine 的绑定之一:

<select data-bind='options: sampleProductCategories, optionsText: "name", optionsCaption: "Select...", value: category' />

这表示下拉列表中的选项应来自 sampleProductCategories。框中的值应为 name 属性,并且在进行选择时,应将其放入 cartLine 的 category 中。

因此,当做出选择时,category 现在等于 sampleProductCategories 数组中的相应对象。类似于:

{
    "products": [{
        "name": "1950's Chicago Surface Lines Streetcar",
        "price": 26.72
    }, {
        "name": "1962 City of Detroit Streetcar",
        "price": 37.49
    }, {
        "name": "Collectable Wooden Train",
        "price": 67.56
    }],
    "name": "Trains"
}

现在,第二个下拉列表绑定如下所示:

<select data-bind='visible: category, options: category() ? category().products : null, optionsText: "name", optionsCaption: "Select...", value: product' />

如果选择了类别,则会显示此绑定。这些选项来自所选类别的 products 数组。当选择一个值时,product observable 将使用数组中的适当对象填充。就像:

{
     "name": "Collectable Wooden Train",
     "price": 67.56
}

所以,下拉菜单一起提供嵌套选项。

The fiddle loads a resource that contains all of the choices that live in the sampleProductCategories array: http://knockoutjs.com/examples/resources/sampleProductCategories.js

Each cartLine object has category and product observables to hold the current choice.

For example, here is one of the bindings for the individual cartLine:

<select data-bind='options: sampleProductCategories, optionsText: "name", optionsCaption: "Select...", value: category' />

This says that the options in the dropdown should come from sampleProductCategories. The value in the box should be the name property and when a selection is made, it should be put into the category of the cartLine.

So, when a selection is made, category now equals the appropriate object from the sampleProductCategories array. Something like:

{
    "products": [{
        "name": "1950's Chicago Surface Lines Streetcar",
        "price": 26.72
    }, {
        "name": "1962 City of Detroit Streetcar",
        "price": 37.49
    }, {
        "name": "Collectable Wooden Train",
        "price": 67.56
    }],
    "name": "Trains"
}

Now, the second dropdown binding looks like:

<select data-bind='visible: category, options: category() ? category().products : null, optionsText: "name", optionsCaption: "Select...", value: product' />

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 the product observable will be populated with the appropriate object from the array. Like:

{
     "name": "Collectable Wooden Train",
     "price": 67.56
}

So, the dropdowns work together to provide nested options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文