0

Is there a way to get a handler to a variable dynamically in JavaScript? I think this won't make sense so here's what I'm trying to do.

I'm using JQuery Flot to generate graphs in my web app. Everything works fine until I have two graphs in the same page.

I get the data from the database using c# and convert it into a JavaScript array and write onto the page like this.

HTML/C#:

@{
        KII.Models.JsonPerformGraphData items = new KII.Models.JsonPerformGraphData();

        foreach (KII.Models.PerformanceGraphItem item in Model.PerformanceGraphItems)
        {
            items.Add(item);
        }

        <script type="text/javascript">

      @Html.Raw("var pgdata"+Model.Id+" = ")  @Html.Raw(items.toJson()) ;

        </script>
    } 

Then, I call a method on document ready to draw the graph

JavaScript:

if ($(".PerformanceGraph")) {
        $(".PerformanceGraph").livequery(function () {
            //Display the graph for risk and reward.
            var options = { series: {
                bars: { show: true, barWidth: 0.45, align: "center" }
            },
                legend: {
                    show: true, position: "ne", backgroundOpacity: 0, container: $("#PerformanceGraphLegend")
                },
                multiplebars: true
            };


            var dataArray = $(this).attr('data-array');//I get the name of the variable as String.
            $.plot($(".PerformanceGraph"), pgdata, options);
        });
    } 

pgdata from javascript must match "var pgdata"+Model.Id+" from HTML/C#.

How can I get the variable that the HTML/C# code spits out to the HTML?

1
  • 1
    Couldn't you just output something like this instead? plotMyJson({json from c#...}); and then create a plotMyJson function in js? Commented Jun 29, 2011 at 18:26

2 Answers 2

1

You could concatenate the variable name together and "eval" it.

http://www.w3schools.com/jsref/jsref_eval.asp

Sign up to request clarification or add additional context in comments.

2 Comments

changing it to $.plot($(this), eval(dataArray), options); worked. I see eval is thought to be evil though.
eval is very evil, and is (almost) never the right way to accomplish whatever you're trying to accomplish.
1

Why not something like this?

<script type="text/javascript">
var pgdata = {};
@Html.Raw("pgdata["+Model.Id+"] = ")  @Html.Raw(items.toJson()) ;
</script>

and then in javascript you simply iterate over the keys in pgdata:

for (k in pgdata) {
    $.plot($(".PerformanceGraph"), pgdata[k], options);
}

2 Comments

The HTML/C# code gets executed several times. I think var pgdata = {}; will clear the array after each execution.
still, this general approach is what you're looking for. Instantiate and assign pgdata somewhere earlier, or do something like if (! ('pgdata' in window)) { pgdata = {}; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.