I often run into a situation like the one exemplified by the code below. The basic idea is to use a single DynamicModule variable as a list or association of values that can be manipulated by individual controls:
n = 5;
DynamicModule[{
var = AssociationThread[Range[5], RandomInteger[10, n]]
},
Column @ Map[
Row[{
Slider[Dynamic[var[#]], {0, 10, 1}],
Dynamic[Echo@var[#], TrackedSymbols :> {var} (* var[#] cannot be used here *)]
},
" "
]&,
Keys[var]
]
]
In this example, whenever one slider is moved, all the Dynamics fire (as you can tell from the Echos) and that is not what I want. I'd like each Dynamic to listen only to changes in var[i]; not to the whole association var.
Of course, I could just localize {var1, var2, var3, ...} instead, but for arbitrary n this gets annoying and difficult to maintain.
I could also wrap each Row in its own DynamicModule that keeps a local copy of var[i] to be used for display purposes only and then modify the Slider to control both var[i] and its copy, but then it becomes quite difficult to make sure the displayed Dynamics behave well when var changes externally (for example, if I want to have a reset button that sets all var[i] to zero).
I'm sure it's technically possible with some sort of meta-programming technique, but is there a more canonical way to do it?