在两个js文件之间共享全局变量 [英] Sharing global variables between two js files

查看:2433
本文介绍了在两个js文件之间共享全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用两个js文件并在钛应用程序中共享它们之间的变量。在我的主app.js中,我有3个变量与表中的每一行相关联。我有一个事件监听器,当单击一行来打开一个模式视图,其组件位于一个单独的js文件中。我的三个变量在下面,在点击事件中,我有3个变量和3个全局变量的提醒。

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

这些都会返回正确的结果。然后在我的第二个js文件中,我也有以下提示:

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

在第二个js文件中第一次点击一行时,我的第二个警报的变量都是未定义的。第二次点击它们都是定义的,但有时是错误的。它似乎给出了我第一次点击的行的变量的结果,这是未定义的。希望这个问题很明确。我做错了什么?

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;

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

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

并且在我的第二个js文件中有这个:

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!

推荐答案

在eventListener中?

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;


因为你的变量是全局的,你需要声明它们eventListener函数之外。例如:

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

如果在App.js中声明,

rowNumber 将是全局的,而

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

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

不会。

这篇关于在两个js文件之间共享全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆