I'm trying to wrap some dynamic code into DynamicModule
to localize all the variables, yet when I do that, the variables stop updating.
That does not happen if I use Module
instead. However, the variables get highlighted in red and when I hover over them with the mouse pointer the following message pops up: "A variable was used where it is probably not going to be evaluated before going out of scope".
The code in question is from Pickett's answer to my earlier question (AJAX-style HTTP calls triggered by a variable change):
url = "https://maps.googleapis.com/maps/api/geocode/json";
callback[_, "data", data_] := choices = extractsAddresses@First@FromCharacterCode[data]
Needs["GeneralUtilities`"]
extractsAddresses[data_] := ToAssociations[ImportString[data, "JSON"]][["results", All, "formatted_address"]]
fetchChoices[addr_] := URLFetchAsynchronous[
url,
callback,
"Parameters" -> {"address" -> addr}
]
Column[{
InputField[
Dynamic[s, (fetchChoices[#]; s = #) &],
String,
ContinuousAction -> True
],
Dynamic@choices
}]
And here is the version wrapped in DynamicModule
:
Needs["GeneralUtilities`"]
DynamicModule[{url, callback, choices = "", fetchChoices, s},
url = "https://maps.googleapis.com/maps/api/geocode/json";
callback[_, "data", data_] :=
choices = extractsAddresses@First@FromCharacterCode[data];
extractsAddresses[data_] :=
ToAssociations[ImportString[data, "JSON"]][["results", All,
"formatted_address"]];
fetchChoices[addr_] :=
URLFetchAsynchronous[url, callback,
"Parameters" -> {"address" -> addr}];
Column[{InputField[Dynamic[s, (fetchChoices[#]; s = #) &], String,
ContinuousAction -> True], Dynamic@choices}]
]
What am I doing wrong?
Dynamic@choices
is not tracking that kind of change. As a quick and dirty work-around, tryDynamic[choices, UpdateInterval->0]
instead. $\endgroup$