I'm trying to get data parsed from CSVs in D3 -- parse once and store the arrays into variable(s) for multiple functions to have access to. I'm working inside a namespace module where the parent vars should make it possible, alas, no luck. Here is a simplified example:
var namespace = namespace || {};
namespace.module = function() {
var dataItems; <== want to be accessible to all functions inside module
function getData() {
d3.csv('data.csv', function(d) {
dataItems = d;
});
}
function drawChartA() {
// want to have access to parsed CSV data here
console.log(dataItems); <== "error: undefined"
}
return {
getData: getData,
drawChartA: drawChartA
}
}();
// run code
$(document).ready(function() {
namespace.module.getData();
namespace.module.drawChartA();
});
The only way I seem to have access the parsed arrays is within the scope the getData() function, but not outside. I even tried calling getData() from inside the drawChartA method, first thing, same result. Another post was suggesting to store the vars under the Window object but shouldn't there be a way to handle it all inside the namespace module? Still learning about all that.. please advise! :)