In my code, I have a specific expression <%= GetAddPinScript() %>
that calls a method located in the backend of my application. This method generates JavaScript code dynamically and returns it, as shown below:
AddPushpin('data', 'more data', numbers, numbers, 'no');
The important part to note here is that the variable 'no'
within the generated JavaScript is crucial for determining whether certain content should be displayed or not in the rest of my code (essentially acting as a boolean).
The issue I'm facing is related to timing - the method in my backend requires a variable (result
in the code snippet provided) to be passed through an AJAX call prior to the evaluation of the expression, so that the correct variable can be generated in the JavaScript. However, the expression evaluates during page load every time.
So, the question is: How can I delay the evaluation of the expression until after the AJAX call has been made?
Below is the relevant JavaScript function:
function displayGeneralInfo(specifics)
{
var callback = AddQueryString(window.location.href, "action", "displayResults");
$.ajax({
url: callback,
type: "POST",
async: false,
data: {
specifics: specifics
}
});
<%= GetAddPinScript() %>
}
and the AddPushpin JavaScript function:
function AddPushpin(name, description, latitude, longitude, selected) {
// Add a pin to the map
var center = new Microsoft.Maps.Location(latitude, longitude);
var pin = new Microsoft.Maps.Pushpin(center, null);
if(selected !== null || selected!="")
{
if(selected == "yes")
{
infoboxOptions = new Microsoft.Maps.Infobox(center,
{ width: 285,
height: 170,
visible:true,
actions:[{label: 'Associate', eventHandler: associate}]
});
map.entities.push(infoboxOptions);
}
}
map.entities.push(pin);
}
Lastly, here's a snippet from the backend code:
public string GetAddPinScript()
{
foreach (Location location in foo(x => !string.IsNullOrWhiteSpace(x.Longitude) && !string.IsNullOrWhiteSpace(x.Latitude)))
{
selected = "no";
if (!result.IsNullOrEmpty())
{
if (location.MapPinDescription.IndexOf(result) > 0)
selected = "yes";
}
pins.Add(string.Format("AddPushpin('{0}', '{1}', {2}, {3}, '{4}');",
location.etc1("'", @"\'"), location.etc2("'", @"\'"), location.etc3, location.etc4, selected));
string retVal = string.Join("\n", pins.ToArray());
return retVal;