2 Replies Latest reply on Jan 8, 2013 10:36 PM by jdhughen

    Problem creating a global "data" var of a json file - need help

    jdhughen Level 1

      I'm having a problem with some code and need some help.
      I'm using the code below to load and parse a JSON file and store the result in a var "data". Everything is working great on the compositionReady page. The problem is it would save a lot of extra loading/parsing time if I could make this "data" avaliable globaly. With the current

       

      var data = eval (json)

       

      the "data" is only available locally. So how do I or can I make this a global var in EDGE ?

       

      Below is what I have now and what I have tried.

       

      $.getJSON('images/profiles.json', function(json)

          {
          //sym.data = eval (json); this didn't work
           var data = eval (json);
          //var temp = eval (json); nor did this pair
          //sym.setVariable ( data, temp)
          for ( i = 1 ; i < 17; i++){
           sym.getSymbol("thumb"+[i]).$("thumb1").css("background-image","url("+data.player[i].thumb +")");
           sym.getSymbol("thumb"+[i]).$("j_num").html(data.player[i].j_num);
           sym.getSymbol("thumb"+[i]).$("p_name").html((data.player[i].f_name)+"<br>"+(data.player[i ].l_name));
           } // the above 3 lines retrives the players thumbnail image, jersey number, and first - last name. this is all working fine
          }
         );
      sym.$("Stage").css({"margin-left":"auto","margin-right":"auto"});

      There are 17 other sym.elements "profile" sheets that will display via "clicking" a animated thumb image. Each sheet has about 20 "fields" that get their data from the JSON file. If it's best to reload/eval the json for each sheet I can do that. It's just seems a better idea to be able to access the same "data" and only load/parse it once??
      If any of you coding gurus could help me out i would appreciate it.

      thanks

      Joel H

        • 1. Re: Problem creating a global "data" var of a json file - need help
          eduard.dudar

          Rough solution is to use data = eval(json). This will create global variable. Not a good solution though. Smarter way is to use setVariable() method: sym.getComposition().getStage().setVariable('data', eval(json)). After that you can use var data = sym.getComposition().getStage().getVariable('data') to access it.

          • 2. Re: Problem creating a global "data" var of a json file - need help
            jdhughen Level 1

            BINGO !

            works like a charm

             

            I used this at compReady. Loads all the animated thumbnails with image, jersey number and full name.

             

            $.getJSON('images/profiles.json', function(json)

                    {

                        sym.getComposition().getStage().setVariable('data', eval(json));

                        var data = sym.getComposition().getStage().getVariable('data');

                        for ( i = 1 ; i < 17; i++){

                            sym.getSymbol("thumb"+[i]).$("thumb1").css("background-image","url("+data.player[i].thumb +")");

                            sym.getSymbol("thumb"+[i]).$("j_num").html(data.player[i].j_num);

                            sym.getSymbol("thumb"+[i]).$("p_name").html((data.player[i].f_name)+"<br>"+(data.player[i ].l_name));

                                                     }

                    }

                );

            sym.$("Stage").css({"margin-left":"auto","margin-right":"auto"});

             

            and this on all the "display on click" profile sheets. Right now just gets the larger player pic but will get all the other "fields" also when complete

             

            var i = 1 // this var identifies the player

            var data = sym.getComposition().getStage().getVariable('data');

            sym.$("player_image").css("background-image","url("+data.player[i].image+")");

            sym.play("profile" + [i]);

             

            Works perfect. Now I just have to add all the other fields to the profile sheets and presto...

             

            thanks again

             

            Joel