新闻资讯APP开发流程(三)-- app.js
如果注释里写的一样,我们将使用单window程序模板,
在app.js里我们主要做几件事情:
-引导程序并载入我们需要的数据
-检测程序运行的设备类型,平台版本或网络连接
-载入并打开顶层的UI组件
我们为android、iphone、ipad平台做判断,根据相应的平台载入相应的程序模板。
而事实上,因为校长追求平台UI的统一性,其实以上操作是多余的,也可以不用,因为所有的UI都是共用的,我们将全程使用view组件,view组件相当于web开发中的div层,所以这样做的结果就是所有平台的UI都是一样的,而且自定义UI样式非常方便,比如说,如果底部使用传统的tabGroup,那么在titanium中你会发现,tab样式是非常难改变的。
并且titanium最佳实践里提到我们尽可能使用单上下文。
app.js
/* * Single Window Application Template: * A basic starting point for your application. Mostly a blank canvas. * * In app.js, we generally take care of a few things: * - Bootstrap the application with any data we need * - Check for dependencies like device type, platform version or network connection * - Require and open our top-level UI component * */ //bootstrap and check dependencies if (Ti.version < 1.8 ) { alert('Sorry - this application template requires Titanium Mobile SDK 1.8 or later'); } // This is a single context application with mutliple windows in a stack (function() { //determine platform and form factor and render approproate components var osname = Ti.Platform.osname, version = Ti.Platform.version, height = Ti.Platform.displayCaps.platformHeight, width = Ti.Platform.displayCaps.platformWidth; //considering tablet to have one dimension over 900px - this is imperfect, so you should feel free to decide //yourself what you consider a tablet form factor for android var isTablet = osname === 'ipad' || (osname === 'android' && (width > 899 || height > 899)); var Window; if (isTablet) { Window = require('ui/tablet/ApplicationWindow'); } else { // Android uses platform-specific properties to create windows. // All other platforms follow a similar UI pattern. if (osname === 'android') { Window = require('ui/handheld/android/ApplicationWindow'); } else { Window = require('ui/handheld/ApplicationWindow'); } } new Window().open(); })();