【问题标题】:Titanium design advice钛设计建议
【发布时间】:2012-10-31 23:26:03
【问题描述】:

在学习 Appcelerator Titanium 时,我正在构建一个以包含 2 个标签的窗口开头的应用程序。这两个标签 (onclick) 应该打开 2 个不同的窗口(每个都包含选项卡组)。

所以,在我的app.js 我有:

Window = require('ui/handheld/ApplicationWindow');

在我的ApplicationWindow 函数中:

    var window1 = Ti.UI.createWindow({
        backgroundColor:'#ffffff'
    });

    var label = Ti.UI.createLabel({ text: "Open first Window", top:10 });
    window1.add(label);

    var label2 = Ti.UI.createLabel({ text: "Open second Window", top:50 });
    window1.add(label2);

    var window2 = Titanium.UI.createWindow({url:"A.js",backgroundColor:'#00ff00'});
    var window3 = Titanium.UI.createWindow({url:"B.js",backgroundColor:'#ff0000'});

    label.addEventListener("click", function(e) {
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP;
    window1.animate({view:window2,transition:t},function(){window2.open();});
    });

    label2.addEventListener("click", function(e) {
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP;
    window1.animate({view:window3,transition:t},function(){window3.open();});
    }); 

    return window1;

第一个问题是:这是一个好的设计吗?是否可以改进?怎么样?

第二个问题是:有没有办法在过渡结束之前显示我正在打开的页面?目前看来A.js和B.js中包含的JS只有在动画停止时才会执行。

谢谢,欢迎任何帮助,对于新手问题表示抱歉。

[编辑] 这是 Ch4rAss 评论后我当前的代码:

function ApplicationWindow() {

    var root = Ti.UI.createWindow({
        backgroundColor:'#ffffff'
    });

    var label = Ti.UI.createLabel({ text: "Open first Window", top:10 });
    root.add(label);

    var label2 = Ti.UI.createLabel({ text: "Open second Window", top:50 });
    root.add(label2);

    var win2 = require('ui/handheld/Win2');
    var win3 = require('ui/handheld/Win3');

    label.addEventListener("click", function(e) {
    var t = Ti.UI.iPhone.AnimationStyle.CURL_UP;
    win2.open({transition:t});
    });

   label2.addEventListener("click", function(e) {
   var t = Ti.UI.iPhone.AnimationStyle.CURL_UP;
    win3.open({transition:t});
   }); 

    return root;
}

module.exports = ApplicationWindow;

和:

function Win2(){
        /* You can (of course) do more than this */
        return Ti.UI.createWindow({backgroundColor:'#00ff00'});
    }
module.exports = Win2;

【问题讨论】:

    标签: javascript titanium appcelerator commonjs


    【解决方案1】:

    您应该在open() 方法中添加动画,以便在动画期间打开窗口:

    window1.open({transition:Titanium.UI.iPhone.AnimationStyle.CURL_UP});
    

    我建议为每个窗口使用单独的文件并使用 CommonJS 来require() 它。与您用于ApplicationWindow 的方法相同。在您创建两个窗口并将它们加载到内存而不使用它们的那一刻!更好的是:

    var Win1 = require('ui/handheld/WhateverWindow');
    label.addEventListener("click", function(e) {
       var win1 = new Win1();
       win1.open({transition:Titanium.UI.iPhone.AnimationStyle.CURL_UP});
    });
    

    还有ui/handheld/WhateverWindow.js

    (function() {
        var WhateverWindow = function() {
            /* You can (of course) do more than this */
            return Ti.UI.createWindow({backgroundColor:'#00ff00'});
        }
        module exports = WhateverWindow;
    })();
    

    您可以阅读更多here

    【讨论】:

    • 这对我不起作用。我对您的代码进行了一些更改,W2.js 文件中的函数 W2 变为:function Win2(){return Ti.UI.createWindow({backgroundColor:'#00ff00'});}module.exports = Win2;。调用代码与您的非常相似,但单击时收到错误消息。我的代码:var win2 =require('ui/handheld/W2');label.addEventListener("click", function(e) {var t = Ti.UI.iPhone.AnimationStyle.CURL_UP;win2.open({transition:t});}); 错误:line = 30; message = "'undefined' 不是一个函数(评估 'win2.open({transition:t})')";名称 = 类型错误; sourceId = 208669216;
    • 也许我不明白,但我等待我们的评论:我必须以这种方式实例化胜利:var Win2 = require('ui/handheld/Win2'); var win2 = 新的 Win2()。然后,win.open(...)...
    • 对不起,我不小心忘记创建一个窗口对象。查看我的编辑。
    • 好的,还有一个问题:如果我想关闭打开的窗口,我注意到我可以使用 close 并传递一个转换(如打开)。我对其进行了测试并且它有效,但我看到根窗口内容仅在过渡结束后出现。有可能改变吗?
    猜你喜欢
    • 2011-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2010-11-17
    相关资源
    最近更新 更多