Sharing global variables between two js files

。_饼干妹妹 提交于 2019-12-24 00:59:25

问题


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.

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);

These are all returning the correct results. Then in my second js file, I also have the following alert:

alert("\n TiAppmyGlobalSongVar in modal is "+Ti.App.myGlobalSongVar+ " \n TiAppmyGlobalArtistVar in modal is "+Ti.App.myGlobalArtistVar + "TiAppmyGlobalRowNumber in modal is "+Ti.App.myGlobalRowNumber);

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?

UPDATE PLEASE READ!!: In the end, after trying:

Titanium.API.titleText = titleText;
Titanium.API.artistText = artistText;

and

Ti.App.Properties.setString('globalTitleText', titleText);
Ti.App.Properties.setString('globalArtistText', artistText);

and

Ti.App.myGlobalSongVar = titleText;
Ti.App.myGlobalArtistVar = artistText;

(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:

Ti.App.fireEvent('myCustomEvent', {
          myTitleText: titleText,
          myArtistText: artistText
        });

and having this in my second js file:

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. 

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!


回答1:


Is the following code IN the eventListener?

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;

Because for your variables to be global, you need to declare them outside the eventListener function. Something like:

var rowNumber;
tableView.addEventListener('click',function(e) {
    rowNumber = e.rowIndex;
}

rowNumber will be global if declared in App.js, whereas:

tableView.addEventListener('click',function(e) {
    var rowNumber;
    rowNumber = e.rowIndex;
}

won't.




回答2:


Pleasee go through this link in titanium developer forum

Global Variables in Titanium

.........................

And it's also best to pass your own object it works in that case.

Passing object as global variable

Please Check it out and let me know

Note : also declare the variables in App.js :) if any value is to be modifies then do it in other JS.



来源:https://stackoverflow.com/questions/18633370/sharing-global-variables-between-two-js-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!