How to pass variables to a deferred infoWindow function

893
5
Jump to solution
05-25-2011 03:46 PM
StephenLead
Regular Contributor III
The sample at http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm#jssamples/widget_infowin... shows how to run a function to format the contents of an infoWindow.

It says "a function (getTextContent) is defined that will execute when a [feature] is clicked" and "In the getTextContent function we have access to the clicked graphic".

The content of the infoWindow is set with:

template.setContent(getTextContent);


and the function references the graphic object using:

function getTextContent(graphic) {


Is it possible to pass other variables into this function, so that they could be used within the function?

Thanks,
Steve
0 Kudos
1 Solution

Accepted Solutions
StephenLead
Regular Contributor III
My brilliant colleague Danny came up with the resolution:

var infoTemplate = new esri.InfoTemplate();
infoTemplate.setTitle("title");
infoTemplate.x = "abc";
infoTemplate.setContent(buildChart);

function buildChart(graphic) {
        var x = this.x;


This allows you to access the value "abc" from within the buildChart function. Genius.

View solution in original post

0 Kudos
5 Replies
derekswingley1
Frequent Contributor
I don't know of a way to do this...I think you're stuck referring to global variables or using a closure for vars you want to access inside your function.
0 Kudos
StephenLead
Regular Contributor III
using a closure for vars you want to access inside your function.


Hi Derek,

Can you elaborate on that suggestion?

Thanks,
Steve
0 Kudos
StephenLead
Regular Contributor III
Actually stack overflow has a good thread which should help me out here:

http://stackoverflow.com/questions/111102/how-do-javascript-closures-work
0 Kudos
derekswingley1
Frequent Contributor
Hey Steve,

I was implying that you could wrap the function you pass to setContent in another function. That way, variables you define in the function that wraps the function passed to setContent are accessible in the setContent function. It's not exactly what you asked for but is one way to avoid using globals.
0 Kudos
StephenLead
Regular Contributor III
My brilliant colleague Danny came up with the resolution:

var infoTemplate = new esri.InfoTemplate();
infoTemplate.setTitle("title");
infoTemplate.x = "abc";
infoTemplate.setContent(buildChart);

function buildChart(graphic) {
        var x = this.x;


This allows you to access the value "abc" from within the buildChart function. Genius.
0 Kudos