简体   繁体   English

在两个js文件之间共享全局变量

[英]Sharing global variables between two js files

I'm working with two js files and sharing variables between them in a titanium app. 我正在使用两个js文件,并在Titan应用程序中在它们之间共享变量。 In my main app.js I have 3 variables associated with each row in a table. 在我的主要app.js中,我有3个变量与表中的每一行相关联。 I have an event listener for when a row is clicked to open a modal view whose components are in a separate js file. 当单击某行以打开其组件位于单独的js文件中的模式视图时,我有一个事件侦听器。 My three variables are below and on the click event I have an alert of the 3 variables and the 3 global variables. 我的三个变量在下面,在单击事件中,我有3个变量和3个全局变量的警报。

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: 然后在我的第二个js文件中,我还有以下警报:

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. 在第二个js文件中第一次单击行时,第二个警报的变量都未定义。 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: 并将其保存在我的第二个js文件中:

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! 我以前在不同的上下文中使用过它们,并且它们确实起作用了,但是在这种特定情况下却没有!

Is the following code IN the eventListener? 以下代码位于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. 因为要使变量具有全局性,所以需要在eventListener函数外部声明它们。 Something like: 就像是:

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

rowNumber will be global if declared in App.js, whereas: 如果在App.js中声明,则rowNumber将是全局的,而:

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

won't. 惯于。

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. 注意:还要在App.js中声明变量:)如果要修改任何值,请在其他JS中进行修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM