简体   繁体   English

如何将js文件添加到Titan移动项目

[英]How do I add a js file to titanium mobile project

I keep getting the following error on a simple Titanium Mobile project: 我在一个简单的Titanium Mobile项目上不断收到以下错误:

Location:
app.js

Message:
Uncaught ReferenceError: tab2 is not defined

Source: tabGroup.addTab(tab2);

Here is the code in my app.js file: 这是我的app.js文件中的代码:

// create tab group
var tabGroup = Titanium.UI.createTabGroup();

// create the window

var win1 = Ti.UI.createWindow({
width: 320,
height: 440,
top: 0,
left: 0,
backgroundImage: 'background.png',
title: 'loan calculator',
barImage: 'navbar.png'
});

 // creat the view, this will hold all of our UI controls
// note the hight of this view is the height of the window
// minus 134px for the status bar and padding and adjusted for navbar

var view = Ti.UI.createView({
width: 300,
height: win1.height - 134,
left: 10,
top: 10,
backgroundColor: '#fff',
borderRadius: 5
});

// we will give the logo a left margin so it centers neatly
// within our view

var _logoMarginLeft = (view.width - 253) / 2;

// now let's add our logo to an imageview and add that to our 
// view object

 var logo = Ti.UI.createImageView({
backgroundImage: 'logo.png',
width: 253,
height: 96,
left: _logoMarginLeft,
top: 0
  });

 view.add(logo);

 // add the view to our window

 win1.add(view);

// add the first tab and attach our window object (win1) to it

var tab1 = Ti.UI.createTab({
icon: 'icon_calculator.png',
title: 'Calculate',
window: win1
  });

 // create the second window for settings tab

 var win2 = Ti.UI.createWindow({
width: 320,
height: 440,
top: 0,
left: 0,
backgroundImage: 'background.png',
url: 'window2.js',
title: 'Settings',
barImage: 'navbar.png'
  });


  // now add the tabs to our tabGroup object

   tabGroup.addTab(tab1);
   tabGroup.addTab(tab2);

  // open the tabgroup to launch the app

  tabGroup.open();

Here is the code in my window2.js: 这是我的window2.js中的代码:

 // add the second tab and attach our external window object
 // (win2 / window2.js) to it

 var tab2 = Ti.UI.createTab({
icon: 'icon_settings.png',
title: 'Settings',
window: win2
});

How can this be resolved? 如何解决?

Why was the tab2 creation moved to window2.js from app.js? 为什么将tab2创建从app.js移到window2.js? What are you trying to accomplish with this change? 您要通过此更改完成什么工作?

Tab1 is constructed correctly... the tabGroup contains the tab (tab1) which was created as a container for the window (window1). Tab1的构造正确... tabGroup包含作为窗口(window1)的容器创建的选项卡(tab1)。 The second tab is being created in the wrong order. 第二个选项卡的创建顺序错误。

Also, when you use the url form of createWindow, it creates a whole new context. 另外,当您使用createWindow的url形式时,它会创建一个全新的上下文。 The items within that window cannot access the parent scope, and vice-versa. 该窗口中的项目无法访问父作用域,反之亦然。

Lastly, as an added bonus, the app.js will likely complete before the window2.js executes. 最后,作为额外的好处,app.js可能会在window2.js执行之前完成。 The URL load is asynchronous, and context creation takes time, so even if it could access across the contexts, tab2 most likely would not yet have been created by the time it was added to the tab group. URL加载是异步的,并且上下文创建需要花费时间,因此即使可以跨上下文访问,也很可能在将tab2添加到选项卡组时尚未创建tab2。 I have had lots of "fun" with those sorts of timing issues... 我对这类计时问题有很多“乐趣”。

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

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