钛-传递变量到新窗口[英] Titanium - Passing Variable to new window

本文是小编为大家收集整理的关于钛-传递变量到新窗口的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在iPhone Titanium应用程序中遇到了一个问题.下面是我的apptabgroup.js文件.有一个名为'transentrance'的活动侦听器.当触发此时,我需要变量事件.Name和event.email传递给所有新选项卡.出于某种原因,这不适用于我.我最终得到了未定义的错误.我想我的问题是我如何设置全局变量?喜欢event.name所以它在应用程序中的任何位置都显示出来.

function AppTabGroup() {
//declare module dependencies
var AppWindow = require('ui/AppWindow');
var OverviewWindow = require('ui/OverviewWindow');
var LeadWindow = require('ui/LeadWindow');
var CaseWindow = require('ui/CaseWindow');
var ResourcesWindow = require('ui/ResourcesWindow');

//create module instance
var self = Ti.UI.createTabGroup();

//create app tabs
var win1 = new OverviewWindow(L('Login')),
    win2 = new CaseWindow(L('Cases'));
    win3 = new LeadWindow(L('Leads'));
    win4 = new ResourcesWindow(L('Resources'));

var tab1 = Ti.UI.createTab({
    title: L('Login'),
    icon: '/images/KS_nav_ui.png',
    window: win1
});
win1.containingTab = tab1;

var tab2 = Ti.UI.createTab({
    title: L('Cases'),
    icon: '/images/KS_nav_views.png',
    window: win2
});
win2.containingTab = tab2;

var tab3 = Ti.UI.createTab({
    title: L('Leads'),
    icon: '/images/KS_nav_views.png',
    window: win3
});
win3.containingTab = tab3;

var tab4 = Ti.UI.createTab({
    title: L('Resources'),
    icon: '/images/KS_nav_mashup.png',
    window: win4
});
win4.containingTab = tab4;

//Load Initial Login tab
self.addTab(tab1);

//If Login is successful then the below even will fire and the other tabs will be loaded
Ti.App.addEventListener('grantEntrance', function(event)  
{
win2.name       = event.name;  
win2.email      = event.email;
self.addTab(tab2);
self.addTab(tab3);
self.addTab(tab4);
self.removeTab(tab1);

});  



return self;
};

module.exports = AppTabGroup;

下面是我的案例window.js选项卡

function AppWindow(title) {
var Main = Ti.UI.createWindow({
    title:title,
    backgroundColor:'white'
});

//Closed Case Button
var ccButton = Titanium.UI.createButtonBar({
    labels:['Closed'],
    backgroundColor:'#336699'
});
Main.setLeftNavButton(ccButton);

//New Case Button
var ncButton = Titanium.UI.createButtonBar({
    labels:['New Case'],
    backgroundColor:'#336699'
});
Main.setRightNavButton(ncButton);

ncButton.addEventListener('click', function() {
    //containingTab attribute must be set by parent tab group on
    //the window for this work
    Main.containingTab.open(Ti.UI.createWindow({
        title: L('newCaseWindow'),
        backgroundColor: 'white'
    }));
});

var msg = Titanium.UI.createLabel({  
text:"Your Name is " + win.name,
//text:"You have successfully logged in. Upon logging in we sent back your email address and your name. You can pass all kinds of data simply by creating objects on your window.\n\nYour email is:\n" + email + "\n\nyour name is:\n" + name,  
top:10,  
left:10,  
width:300,  
height:'auto'  
});  
    Main.add(msg);  

    return Main;
};

module.exports = AppWindow;
变量应在所有其他JS文件中使用.您应该为这些全局变量创建一个命名空间,以确保它们不会干扰任何其他变量.

这是你如何做到的例子.在app.js中:

var MyGlobalVars = {
    email: null,
    name: null
};

然后,您可以通过引用来获取并设置应用程序中的任何位置的这些值:

MyGlobalVars.email = 'me@gmail.com';
alert('My email is: '+MyGlobalVars.email);

但是,这些值不会存储在您应用的执行之间.如果您停止您的应用程序并再次启动它,那么在您再次设置它们之前将丢失这些变量.如果您要存储信息,以便在应用程序重新启动后再次访问它们,可能使用TI.App.Properties存储它们.

有关在此存储信息的不同方式的信息:

https://wiki.appcelerator.org/display/指南/工作+与+本地+数据+源

本文地址:https://www.itbaoku.cn/post/1938207.html

问题描述

I am having a problem passing variables in my iphone titanium App. Below is my AppTabGroup.js file. There is an event listener called 'grantEntrance'. When this is fired I need the variables event.name and event.email passed to all the new tabs. For some reason this is not working for me. I end up getting undefined errors. I think my question is how do I set global variables? Like event.name so it shows up anywhere in the application.

function AppTabGroup() {
//declare module dependencies
var AppWindow = require('ui/AppWindow');
var OverviewWindow = require('ui/OverviewWindow');
var LeadWindow = require('ui/LeadWindow');
var CaseWindow = require('ui/CaseWindow');
var ResourcesWindow = require('ui/ResourcesWindow');

//create module instance
var self = Ti.UI.createTabGroup();

//create app tabs
var win1 = new OverviewWindow(L('Login')),
    win2 = new CaseWindow(L('Cases'));
    win3 = new LeadWindow(L('Leads'));
    win4 = new ResourcesWindow(L('Resources'));

var tab1 = Ti.UI.createTab({
    title: L('Login'),
    icon: '/images/KS_nav_ui.png',
    window: win1
});
win1.containingTab = tab1;

var tab2 = Ti.UI.createTab({
    title: L('Cases'),
    icon: '/images/KS_nav_views.png',
    window: win2
});
win2.containingTab = tab2;

var tab3 = Ti.UI.createTab({
    title: L('Leads'),
    icon: '/images/KS_nav_views.png',
    window: win3
});
win3.containingTab = tab3;

var tab4 = Ti.UI.createTab({
    title: L('Resources'),
    icon: '/images/KS_nav_mashup.png',
    window: win4
});
win4.containingTab = tab4;

//Load Initial Login tab
self.addTab(tab1);

//If Login is successful then the below even will fire and the other tabs will be loaded
Ti.App.addEventListener('grantEntrance', function(event)  
{
win2.name       = event.name;  
win2.email      = event.email;
self.addTab(tab2);
self.addTab(tab3);
self.addTab(tab4);
self.removeTab(tab1);

});  



return self;
};

module.exports = AppTabGroup;

Below is my CaseWindow.js tab

function AppWindow(title) {
var Main = Ti.UI.createWindow({
    title:title,
    backgroundColor:'white'
});

//Closed Case Button
var ccButton = Titanium.UI.createButtonBar({
    labels:['Closed'],
    backgroundColor:'#336699'
});
Main.setLeftNavButton(ccButton);

//New Case Button
var ncButton = Titanium.UI.createButtonBar({
    labels:['New Case'],
    backgroundColor:'#336699'
});
Main.setRightNavButton(ncButton);

ncButton.addEventListener('click', function() {
    //containingTab attribute must be set by parent tab group on
    //the window for this work
    Main.containingTab.open(Ti.UI.createWindow({
        title: L('newCaseWindow'),
        backgroundColor: 'white'
    }));
});

var msg = Titanium.UI.createLabel({  
text:"Your Name is " + win.name,
//text:"You have successfully logged in. Upon logging in we sent back your email address and your name. You can pass all kinds of data simply by creating objects on your window.\n\nYour email is:\n" + email + "\n\nyour name is:\n" + name,  
top:10,  
left:10,  
width:300,  
height:'auto'  
});  
    Main.add(msg);  

    return Main;
};

module.exports = AppWindow;

推荐答案

Variables defined in the app.js file should be available in all other JS files. You should create a namespace for these global variables however, to make sure that they don't interfere with any other variables.

Here's an example of how you might do it. In app.js:

var MyGlobalVars = {
    email: null,
    name: null
};

Then you can get and set these values from anywhere in your application by referencing:

MyGlobalVars.email = 'me@gmail.com';
alert('My email is: '+MyGlobalVars.email);

However, these values won't be stored in between executions of you app. If you stop your app, and start it again, those variables will be lost until you set them again. If you are looking to store the information so that you can access them again after application restart, perhaps use the Ti.App.Properties to store them.

There is information on the different ways to store information here:

https://wiki.appcelerator.org/display/guides/Working+with+Local+Data+Sources