title: variable scope inside of d3 reusable API
body: <p>I am trying to create a reusable data manager API for a reusable chart based on the book "Developing a d3.js Edge". 
  you can find the code I am inspiring from in this <a href="https://github.com/GerHobbelt/Developing-a-D3.js-Edge/blob/master/code/chapter-06-12/08.01.drawRoutes/javascripts/transport.js" rel="nofollow">Github repository</a>. </p>
  
  <p>So the data manager API goes like this: </p>
  
  <pre><code> kdd.dataManager = function module(){
  
  var exports = {}, 
      dispatch = d3.dispatch('dataReady', 'dataLoading'), 
      data; 
  
  exports.loadJsonData = function(_uri, _callback){
      d3.json(_uri, function(_err, _data){
          _callback(_data);
      });
  };
  
  exports.loadCsvData = function(_file, _cleaningFunc){
  
      if(!_cleaningFunc) _cleaningFunc = function(d){
          return d;
      };
  
      var loadCsv = d3.csv(_file); 
  
      loadCsv.on('progress', function(){
          dispatch.dataLoading(d3.event.loaded); 
      }); 
  
      loadCsv.get(function(_err, _response){
  
          _response.forEach(function(d){
              _cleaningFunc(d);
          }); 
  
          // assign the data response to our data variable 
          data = _response;
          dispatch.dataReady(_response); 
      }); 
  }; 
  
  //access the data 
  exports.getCleanedData = function(){
      return data; 
  }; 
  
  
  d3.rebind(exports, dispatch, 'on'); 
  return exports; 
  }; 
  </code></pre>
  
  <p>and the problem is that when I use the getCleanedData() method I get "undefined", and I only get the right data when I console.log the data variable inside exports.loadCsv.get().</p>
  
  <pre><code>    var newDataManager = kdd.dataManager(); 
  
  var file= "data/sp500.csv"; 
  
  newDataManager.loadCsvData(file); 
  console.log(newDataManager.getCleanedData());
  </code></pre>
  
  <p>I can actually pass a callback function to the get function that would apply some logic when the data is loaded. But normally considering the variable scope within the name space the data variable should be accessible from outside the loadCsv.get method. </p>
  
  <p>Could you please check what's wrong! 
  Thanks &amp; regards
  Mohamed Ali </p>