在Titanium项目中使用Alloy和CommonJS组织JS代码

时间:2013-08-27 08:46:43

标签: titanium appcelerator commonjs titanium-alloy

我正在学习使用Titanium制作iPhone / Android应用程序。我正在使用Alloy MVC框架。我之前从未使用过javascript,除了HTML中的简单脚本来访问DOM或类似的东西,所以我从来不需要构建代码。

现在,使用Titanium,我必须使用大量的JS代码,而我正在寻找构建代码的方法。基本上我找到了3种方法:函数内的原型,命名空间和函数。

每个的简单示例:

原型

NavigationController = function() {
    this.windowStack = [];
};

NavigationController.prototype.open = function(windowToOpen) {
    //add the window to the stack of windows managed by the controller
    this.windowStack.push(windowToOpen);

    //grab a copy of the current nav controller for use in the callback
    var that = this;
    windowToOpen.addEventListener('close', function() {
        if (that.windowStack.length > 1)
        {
            that.windowStack.pop();
        }
    });

    if(Ti.Platform.osname === 'android') {
        windowToOpen.open();
    } else {
        this.navGroup.open(windowToOpen);
    }
};

NavigationController.prototype.back = function(w) {
    //store a copy of all the current windows on the stack
    if(Ti.Platform.osname === 'android') {
        w.close();
    } else {
        this.navGroup.close(w);
    }
};

module.exports = NavigationController;

将其用作:

var NavigationController = require('navigator');
var navController = new NavigationController();

命名空间(或者我认为是这样的,因为使用了我= {}):

exports.createNavigatorGroup = function() {
    var me = {};

    if (OS_IOS) {   
        var navGroup = Titanium.UI.iPhone.createNavigationGroup();  
        var winNav = Titanium.UI.createWindow();
        winNav.add(navGroup);

        me.open = function(win) {
            if (!navGroup.window) {
                // First time call, add the window to the navigator and open the navigator window
                navGroup.window = win;
                winNav.open();
            } else {
                // All other calls, open the window through the navigator
                navGroup.open(win);
            }
        };

        me.setRightButton = function(win, button) {
            win.setRightNavButton(button);
        };

        me.close = function(win) {
            if (navGroup.window) {
                // Close the window on this nav
                navGroup.close(win);
            }
        };
    };

    return me;
};

将其用作:

var ui = require('navigation');
var nav = ui.createNavigatorGroup();

功能内的功能

function foobar(){
    this.foo = function(){
        console.log('Hello foo');
    }

    this.bar = function(){
        console.log('Hello bar');
    }
}

// expose foobar to other modules
exports.foobar = foobar;

将其用作:

var foobar = require('foobar').foobar
var test = new foobar();

test.bar(); // 'Hello bar'

现在我的问题是:维护代码清洁和清除哪个更好?看起来原型清晰易读/保持。命名空间让我感到困惑,但只需要执行初始函数“可用”(声明它时不使用new,我想因为它返回对象?命名空间?“我”)。最后,函数内部的函数与上一个函数类似,所以我不确切地知道它们之间的区别,但是只导出main函数并且所有内部函数都可以在以后使用它。

也许最后两种可能性是一样的,而且我弄乱了概念。

请记住,我正在寻找一种很好的方法来构建代码,并将功能提供给其他模块以及自己的模块。

我感谢任何澄清。

1 个答案:

答案 0 :(得分:1)

在他们发布的示例中,Appcelerator似乎遵循非原型方法。您可以在他们发布的示例中看到它:https://github.com/appcelerator/Field-Service-App

在Alloy之前,我已经看到很多不同的方法在Titanium中构建应用程序。自从Alloy,我发现开发团队的例子对我有帮助。

话虽如此,在我看来,所有这些仍然在解释之中,并且对改变和社区发展持开放态度。在合金之前,有一些很好的社区建议来构建一个应用程序,我相信它仍然是合金的开放。通常当我找到某人的示例代码时,我会看到他们用它做的事情似乎比我想象的要好一些。它似乎使它更容易。

我认为你应该以对你有意义的方式构建你的应用程序。您可能会偶然发现使用Alloy开发应用程序的更好,更简单的方法,因为您正在批判性地看待它。

我没有找到很多广泛的合金示例,但Field-Service-App对我来说很有意义。它们将应用程序中的元素与MVC区分开来。看看吧。

相关问题