如何在经典钛中正确初始化窗口



我正在尝试定义一个新窗口,以便在iOS的Classic Titanium应用程序中使用。为了使窗口正常工作,需要进行一些预处理。此预处理包括创建 UI 组件(如标签、表和按钮),--,组装此组件,以及添加事件侦听器。但是,我不认为这是最好的方法。有什么见解吗?

function LandingPage() {
    this.prepareForExecution();
}
LandingPage.prototype = {
    constructor: LandingPage,
    _proxy: Ti.UI.createWindow({
        title: "To Do List",
        backgroundColor: "#FFFFFF",
        navBarHidden: false,
        orientationModes: [
            Ti.UI.PORTRAIT,
            Ti.UI.LANDSCAPE_RIGHT,
            Ti.UI.LANDSCAPE_LEFT
        ]
    }),
    get proxy() {
        return this._proxy;
    },
    prepareForExecution: function () {
        this.createUIcomponents();
        this.buildUserInterface();
        this.addEventListeners();
        this.populateTaskTable();
    },
    ...
};

受合金启发 我会为每个窗口使用一个 CommonJS 模块,代码如下:

module.exports = function Controller() {
  var $ = this;
  // if you need views accessable assign them to $
  $.index = Ti.UI.createWindow({
    title: "To Do List",
    backgroundColor: "#FFFFFF",
    navBarHidden: false,
    orientationModes: [
      Ti.UI.PORTRAIT,
      Ti.UI.LANDSCAPE_RIGHT,
      Ti.UI.LANDSCAPE_LEFT
    ]
  });
  // do whatever pre-processing you need
  // interface to get a specific or main view
  $.getView = function getView(id) {
    return $[id || 'index'];
  };
};

我喜欢为此使用模块模式和钛导出。

以下是最近一个项目的示例:

意见.js

var HubWindow = (function () {
    var homeWindow = Ti.UI.createWindow({
        height: deviceHeight,
        width: deviceWidth,
        layout:'vertical',
        backgroundColor: '#ffffff'
    });
    var addView = function(){
        for(var i=0; i<arguments.length; i++){
            homeWindow.add(arguments[i]);
        }
    };
    var openWindow = function(){
        homeWindow.open();
    };
    return{
        add: addView,
        open: openWindow
    };
})();
exports.HubWindow = HubWindow;

应用.js

var App = (function(){
    var Views = require('views');
    var launch = function(){
        Views.HubWindow.open();
        ...
    };
    return {
        launch: launch
    }
})();
App.launch();

我为将以编程方式创建的视图保存原型。

最新更新