如何将js文件添加到titanium mobile项目中

发布于 2024-12-29 17:25:21 字数 2006 浏览 1 评论 0原文

我在一个简单的 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
});

如何解决此问题?

I keep getting the following error on a simple Titanium Mobile project:

Location:
app.js

Message:
Uncaught ReferenceError: tab2 is not defined

Source: tabGroup.addTab(tab2);

Here is the code in my app.js file:

// 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:

 // 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻许诺言 2025-01-05 17:25:21

为什么 tab2 创建从 app.js 转移到 window2.js?您想通过此更改实现什么目标?

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

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

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

Why was the tab2 creation moved to window2.js from app.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). 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. 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. 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. I have had lots of "fun" with those sorts of timing issues...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文