When I'm creating DynamicModule
s for CDF purposes I'm usualy basing on my experience with that, particular goal and content rather than on deep insight.
I want to ask for advice what is a good place for definitions of rather non-dynamic parts of the code. More precise question is at the end so you can scroll down and come back later :)
I'm going to show couple of ways, their advantages and disadvantages.
Initialization :>
DynamicModule[{}, st1@"title"
, Initialization :> (st1 = Style[#, Bold] &)]
st1["title"]
Well, that's not what we want.
I know I can use
Dynamic
but here and after we are assuming that the less insideDynamic
the better. There is no special need for it so I will not do this.Notice that we can Shift+Enter again and saving such
DynamicModule
will result in proper display of content. Double evaluation is the only inconveniece.
scoped Initialization :>
DynamicModule[{st1}, st1@"title"
, Initialization :> (st1 = Style[#, Bold] &)]
st1$$["title"]
It is natural one may want to scope this name but now it is not going to work even with double evaluation.
Initialization ->
DynamicModule[{}, st2@"title"
, Initialization -> (st2 = Style[#, Bold] &)
]
title
Whoah, **->**
seems to be a solution... nope. Unfortunately I can't recall the example but I've faced a problem when I tried to switch :>
to ->
once in the past. The CDF was build with :>
and ->
broke it.
Moreover, if we try to scope the name it is going to fail:
scoped Initialization ->
DynamicModule[{st2}, st2@"title"
, Initialization -> (st2 = Style[#, Bold] &)
]
st2$$["title"]
Body of DynamicModule
This is the way I'm using for defining static parts of the code. It works for scoped and unscoped names:
DynamicModule[{st2},
st2 = Style[#, Bold] &;
st2@"title"
]
title
However there is one issue discussed here. We can not use
SetDelayed
but we assumed it should be a static content so it doesnt matter (or at least I can't recall any counterexample).This is also not very convenient if there is a lot of static content one want to define. I like to have body skeleton on top so it is easy to manage the whole project easily. I'm afraid I can't have both. ;)
Outisde
In case of static elements that are imidiately on top there is also a way to define them outside DynamicModule
. FE is going to store them and such CDF will work. However not everything is on top at start.
Questions
Main question is:
- Is the habit to define static content in
Body
and dynamic parts inInitialization
the best approach?
Secondary qestion:
- What is the difference between
->
and:>
withInitialization
, is one better from another? Documentation center says:"The typical usage is
Initialization:>expr
."
but on the other hand InputAutoReplacement
puts Initialization->
.
st2
in anyDynamic
contexts). $\endgroup$