1 Reply Latest reply on Jul 24, 2017 10:02 AM by Greg Dettmer

    Making variables global in WDC

    Greg Dettmer

      Hello,

       

      I'm currently working on a WDC that's supposed to pull off project data from ppmroadmap.com. Each project entered on the website has specific milestones that I want to access, but the website's API calls only allow me to pull off milestones for one project at a time based on its 5-digit project ID. My solution was to make one getJSON call to pull off all of the project IDs and put them in an array, and then iterate through this table to pull off the milestones for each project based on their unique URLs. However, when I try to access the array that I pushed all the project IDs into from my first getJSON call, I get an error saying the array is undefined. I think it's an issue with the array being a local variable within the function(resp) scope as defined in the WDC tutorial, and I can't figure out how to access it outside of the function. I've already looked at tableau.connectionData but it seems there should be a simpler solution.

       

      Thanks

        • 1. Re: Making variables global in WDC
          Greg Dettmer

          EDIT: I figured it out...

           

          The issue was that $.getJSON calls are inherently asynchronous. If you're a novice Javascript editor like me, this means that code outside the $.getJSON response function will continue to run even if all the lines inside the function have not yet been executed. This caused problems because my second $.getJSON call was attempting to extract Project IDs from a table that had not yet been created in the function above it, leading to an undefined variable error. I thought this had to do with making the variable global instead of local, so I defined the variable at the beginning of my script, and though this eliminated the error, it did not solve my problem as now I was just trying to extract project IDs from an empty table rather than a non-existent one. In order to fix my problem, I had to specify in my $.ajaxSetup to run my $.getJSON response functions synchronously . This is done by writing "$.ajaxSetup({async: false}); Although in general running ajax requests synchronously is not recommended and takes longer, this solution worked for me because my WDC does not require user interaction and the data is only updated once every few days. Hope this helps anyone experiencing similar issues!

          1 of 1 people found this helpful