[英]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.