繁体   English   中英

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

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

我在一个简单的Titanium Mobile项目上不断收到以下错误:

Location:
app.js

Message:
Uncaught ReferenceError: tab2 is not defined

Source: tabGroup.addTab(tab2);

这是我的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();

这是我的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
});

如何解决?

为什么将tab2创建从app.js移到window2.js? 您要通过此更改完成什么工作?

Tab1的构造正确... tabGroup包含作为窗口(window1)的容器创建的选项卡(tab1)。 第二个选项卡的创建顺序错误。

另外,当您使用createWindow的url形式时,它会创建一个全新的上下文。 该窗口中的项目无法访问父作用域,反之亦然。

最后,作为额外的好处,app.js可能会在window2.js执行之前完成。 URL加载是异步的,并且上下文创建需要花费时间,因此即使可以跨上下文访问,也很可能在将tab2添加到选项卡组时尚未创建tab2。 我对这类计时问题有很多“乐趣”。

暂无
暂无

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

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