2013-08-27 102 views
3

我正在学习使用Titanium制作iPhone/Android应用程序。我正在使用Alloy MVC框架。除了HTML中的简单脚本来访问DOM或类似的东西外,我从来没有使用过JavaScript,所以我从来不需要构造代码。在Titanium项目中使用Alloy和CommonJS组织JS代码

现在,与钛,我必须使用大量的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' 

现在我的问题是:哪个是更好的保持代码清洁和清晰?看起来原型很容易读懂/保守。命名空间使我困惑一些,但只需要执行初始函数以“可用”(在声明它时不使用新的,我想是因为它返回对象?命名空间?“我”)。最后,函数内部的函数与最后一个相似,所以我不清楚它们的区别,但是仅导出主函数并且可以在后面使用所有内部函数。

也许最后两种可能性是相同的,我搞乱了概念。

请记住,我正在寻找一种构建代码的好方法,并且可以为其他模块提供功能,也可以在自己的模块中使用这些功能。

我很感激任何澄清。

回答

1

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

我见过很多不同的方法来构造合金前的钛应用程序。自从Alloy以来,我发现开发团队的例子对我有帮助。

就这样说,在我看来,所有这些仍在解释之中,并且对改变和社区发展持开放态度。在合金之前,有关于构建应用程序的一些非常好的社区建议,我相信它仍然与Alloy合作。通常当我找到某人的示例代码时,我看到他们对它做了一些似乎比我想象的更好的组织。它似乎使它更容易一些。

我认为你应该以对你有意义的方式构建你的应用程序。您可能会偶然发现一种更好,更简单的方式来开发Alloy的应用程序,因为您正在严格审视它。

我还没有找到很多广泛的合金例子,但Field-Service-App对我来说很有意义。它们在MVC之外的应用程序中有很好的分离。一探究竟。

+0

嗨,@马丁。感谢您的答复。我对你推荐的这个项目做了一点点看法,它似乎使用了“函数内部的函数”方法。正如你所说,最重要的是对选择感到满意。现在我正在使用原型,我觉得很清楚,但会看到是否有更多的意见。 – Eagle