title: Sharing global variables between two js files
body: <p>I'm working with two js files and sharing variables between them in a titanium app. In my main app.js I have 3 variables associated with each row in a table. I have an event listener for when a row is clicked to open a modal view whose components are in a separate js file. My three variables are below and on the click event I have an alert of the 3 variables and the 3 global variables.</p>
  
  <pre><code>var titleText = titleInRow.text;
  var artistText=artistInRow.text;
  
  Ti.App.myGlobalSongVar = titleText;
  Ti.App.myGlobalArtistVar = artistText;
  
  var rowNumber=e.row.name;
  Ti.App.myGlobalRowNumber= rowNumber;
  
  alert("titleText is: "+titleText+" and /n artistText is "+artistText+ " and /n row number is "+rowNumber +"/n TiAppmyGlobalSongVar is "+Ti.App.myGlobalSongVar+ " /n TiAppmyGlobalArtistVar is "+Ti.App.myGlobalArtistVar);
  </code></pre>
  
  <p>These are all returning the correct results. Then in my second js file, I also have the following alert:</p>
  
  <pre><code>alert("\n TiAppmyGlobalSongVar in modal is "+Ti.App.myGlobalSongVar+ " \n TiAppmyGlobalArtistVar in modal is "+Ti.App.myGlobalArtistVar + "TiAppmyGlobalRowNumber in modal is "+Ti.App.myGlobalRowNumber);
  </code></pre>
  
  <p>In the second js file The first time I click on a row, my second alert's variables are all undefined. The second time I click they are all defined but sometimes wrong. It seems to give the results of the variables for the row I first clicked which was undefined. Hope this question was clear. What am I doing wrong?</p>
  
  <p><strong>UPDATE PLEASE READ!!:</strong>
  In the end, after trying: </p>
  
  <pre><code>Titanium.API.titleText = titleText;
  Titanium.API.artistText = artistText;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Ti.App.Properties.setString('globalTitleText', titleText);
  Ti.App.Properties.setString('globalArtistText', artistText);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>Ti.App.myGlobalSongVar = titleText;
  Ti.App.myGlobalArtistVar = artistText;
  </code></pre>
  
  <p>(which ALL worked the second time, but were undefined the first),
  the only thing which worked was firing this event in my table event listener: </p>
  
  <pre><code>Ti.App.fireEvent('myCustomEvent', {
            myTitleText: titleText,
            myArtistText: artistText
          });
  </code></pre>
  
  <p>and having this in my second js file:</p>
  
  <pre><code>var globalTitleText;
  var globalArtistText;
      Ti.App.addEventListener('myCustomEvent', function(event) {
              globalTitleText=event.myTitleText;
              globalTitleText=event.myTitleText;
              //Ti.App.globalTitleText=event.myTitleText;
              //Ti.App.globalArtistText=event.myArtistText;
              alert('You sent me: '+event.myTitleText+" and "+event.myArtistText);
      });
  
  //However I can't use it here in my second js file (outside the custom event listener) as it is undefined. 
  </code></pre>
  
  <p>Can anyone help me with the last bit of the problem? 
  I still don't know why the other methods didn't work. I've used them in different contexts before and they did work, but not in this particular instance!</p>