Hi
I have a very typical problem which I believe might be more easily solvable
if it were designed better:
I have 3 dropdowns: dropdown1 (State), dropdown2 (County), dropdown3 (crop).
So the idea is that each state has different counties and different
permutations of state and counties lead to different sets of crops. I have a
total of 63 combinations of sets of crops available right now. Each of these
combinations can consist of one or more of the following: (crop1, crop2,
crop3, crop4, crop5, crop6, crop7). Right now each of these combinations is
hardcoded.
I would like to improve the design of the code if it is possible. The way I
see this problem is that each of those 63 combinations is just adding
different kinds of condiments (crops in our case!) depending on the state
and county selected by the user. This looks something like the decorator
pattern where we can decorate(add accessories) objects at runtime.
However I am not entirely sure if first this is a good example of decorator
pattern and secondly if it is possible to implement something like this in
Javascript. Any thoughts/ideas/suggestions would be welcome...
Navodit
p.s.: Some sample code to give you an idea of what I am talking about-
if (document.Insur ance.State.sele ctedIndex == 1 &&
(document.Insur ance.county.sel ectedIndex == 1 ||
document.Insura nce.county.sele ctedIndex == 2 ||
document.Insura nce.county.sele ctedIndex == 100 ||
document.Insura nce.county.sele ctedIndex == 101))
{
document.Insura nce.Commodity.l ength = 5;
document.Insura nce.Commodity[1].value = "4116";
document.Insura nce.Commodity[1].text = "Corn";
document.Insura nce.Commodity[2].value = "81997";
document.Insura nce.Commodity[2].text = "Soybeans";
document.Insura nce.Commodity[3].value = "11997";
document.Insura nce.Commodity[3].text = "Wheat";
document.Insura nce.Commodity[4].value = "51997";
document.Insura nce.Commodity[4].text = "Grain sorghum";
}
else if (document.Insur ance.State.sele ctedIndex == 1 &&
(document.Insur ance.county.sel ectedIndex == 4 ||
document.Insura nce.county.sele ctedIndex == 6 ||
document.Insura nce.county.sele ctedIndex == 94 ||
document.Insura nce.county.sele ctedIndex == 102))
{
document.Insura nce.Commodity.l ength = 4;
document.Insura nce.Commodity[1].value = "4116";
document.Insura nce.Commodity[1].text = "Corn";
document.Insura nce.Commodity[2].value = "81997";
document.Insura nce.Commodity[2].text = "Soybeans";
document.Insura nce.Commodity[3].value = "11997";
document.Insura nce.Commodity[3].text = "Wheat";
}
else if .....
{
}
There are in total 63 different else if combinations... .