简体   繁体   English

如何通过剔除一般挂钩依赖下拉菜单

[英]How to generically hook up dependent drop-downs with knockout

I have a json structure like the following: 我有一个像下面这样的json结构:

var scenario = {
    paints: [
       {
            product: 'Super Paint',
            sheens: [
                { sheen: 'Satin', cost: 42 },
                { sheen: 'Semi-Gloss', cost: 50 }
            ]
        },
        {
            product: 'Cashmere',
            sheens: [
                { sheen: 'Flat', cost: 42 },
                { sheen: 'Medium Lustre', cost: 50 }
            ]
        }
    ],
    walls: {
      "sheen":"Flat",
      "paintProduct":"Cashmere",
    },
    ceiling: {
      "sheen":"Flat",
      "paintProduct":"Cashmere",
   }
};

I figured out now to make drop-downs dependent for a given paintable item with the following: 我现在想出要使下拉列表与给定的可绘画项目相关的方法如下:

function getSheens(paintProduct) {
    if (paintProduct) {
        for (var i = 0; i < self.scenario.paints.length; ++i) {
            if (self.scenario.paints[i].product === paintProduct) {
                return self.scenario.paints[i].sheens;
            }
        }
    }
    return null;
}

self.scenario.walls.sheens = ko.computed(function() {
    return getSheens(self.scenario.walls.paintProduct());
});

self.scenario.ceiling.sheens = ko.computed(function() {
    return getSheens(self.scenario.ceiling.paintProduct());
});

Here's the html: 这是html:

<div class="item edit-walls" data-bind="with: scenario.walls">
    <label>Product</label>
    <select class="form-control paint-product" data-bind="options:$parent.scenario.paints, optionsText:'product', optionsValue:'product', value:paintProduct"></select>

    <label>Sheen</label>
    <select class="form-control paint-sheen" data-bind="options:$parent.scenario.walls.sheens, optionsText:'sheen', optionsValue:'sheen', value:sheen"></select>
</div>

The changing of the paint product for a given item, should reload the sheens of the newly select paint product. 给定项目的涂料产品更改应重新加载新选择的涂料产品的光泽。

The selected values for the paint product and sheen for the item, ie walls, are stored in the walls object. 油漆产品的选择值和项目(即墙)的光泽存储在墙对象中。

What I would like to avoid is the repeating of the computed knockout call for each different item type. 我要避免的是针对每种不同的物料类型重复计算的淘汰赛电话。 Is there a way to have this happen globally and pass in the context somehow? 有没有办法使这种情况在全球范围内发生并以某种方式传递到上下文中?

I'm using knockout.js, knockout.viewmodel.js and jQuery. 我使用的是kickout.js,knockout.viewmodel.js和jQuery。

You're basically asking if it's possible to normalize "walls" and "ceiling" into one concept. 您基本上是在问是否有可能将“墙”和“天花板”标准化为一个概念。 You surely can. 当然可以。 Let's call them paintables . 我们称它们为paintables Change your data structure to something like this: 将数据结构更改为以下形式:

var scenario = {
    paints: [/* omitted */],
    paintables: [
        { 
          "name":"walls",
          "sheen":"Flat",
          "paintProduct":"Cashmere"
        },
        { 
          "name":"ceiling",
          "sheen":"Flat",
          "paintProduct":"Cashmere"
    }]
};

You can then have a Paintable constructor function which implements the logic you have for sheen s in a DRY manner. 然后,您可以有一个Paintable ,它实现了你的逻辑构造函数sheen在干燥方式秒。

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

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