简体   繁体   English

如何在Appcelerator Titanium项目中组织JS文件

[英]How to organize JS files in a Appcelerator Titanium project

I have recently started creating an iPhone application using Appcelerator's Titanium. 我最近开始使用Appcelerator的Titanium创建一个iPhone应用程序。 Since the application is essentially all JS, I needed some advice on how I should organize this project. 由于应用程序基本上都是JS,我需要一些关于如何组织这个项目的建议。

It's becoming very easy to just create long procedural files for each view in the application. 为应用程序中的每个视图创建长程序文件变得非常容易。 Is there a way I can incorporate MVC, or some structure to the project? 有没有办法将MVC或某些结构合并到项目中?

Thanks, I appreciate it. 谢谢,我很感激。 -Tilo -Tilo

Titanium itself is essentially MVC given that your app.js file is the main controller and each View you create is the view and you pass (or set) model data against the view. Titanium本身就是MVC,因为你的app.js文件是主控制器,你创建的每个View都是视图,你可以对视图传递(或设置)模型数据。

In Titanium, you can decompose your application using a couple of nice built-in mechanisms: 在Titanium中,您可以使用几个不错的内置机制来分解您的应用程序:

  1. Titanium.include - Titanium.include allows you to include one or more JS files in place much like the C #include compiler directive. Titanium.include - Titanium.include允许您包含一个或多个JS文件,就像C #include编译器指令一样。 You can put common functions and JS classes in this file and then include them where ever you want them imported and available. 您可以将常用函数和JS类放在此文件中,然后将它们包含在您希望它们导入和可用的位置。

  2. Titanium.UI.createWindow - you can create a new View as as a property of the new Window pass in a URL to another JS context which will create a new JS sub-context and allow you to maintain its own variable space (but still give you access back to your parent). Titanium.UI.createWindow - 您可以创建一个新的View作为新Window的属性传递到另一个JS上下文的URL,这将创建一个新的JS子上下文并允许您维护自己的变量空间(但仍然给出您可以访问您的父母)。

Also, in Titanium, you can create folders that allow you to logically organize your application in a way that is suitable to you and your application. 此外,在Titanium中,您可以创建文件夹,以便以适合您和您的应用程序的方式逻辑地组织应用程序。

Edit: Today, the Titanium.Include method is deprecated. 编辑:今天,不推荐使用Titanium.Include方法。 As mentionned in the documentation, we should create a CommonJS module and use the require() statement. 如文档中所提到的,我们应该创建一个CommonJS模块并使用require()语句。

More information about this statement : Require 有关此声明的更多信息: 要求

More information about modules : Modules 有关模块的更多信息: 模块

As I was not finding an appropriate MVC solution for a Titanium mobile project, I came up with the following approach. 由于我没有为Titanium移动项目找到合适的MVC解决方案,我想出了以下方法。 For small apps this might be over-engineered but could help for maintaining growing applications. 对于小型应用程序,这可能过度设计,但可以帮助维护不断增长的应用程序。

Folder structure: 文件夹结构:

/Resources
  /model
  /view
  /controller
  /ui
  /iphone
  /android
  app.js
  app.jss

For separating views, models and controllers a namespace is needed, so we define it in the app.js, which is our main controller: 为了分离视图,模型和控制器,需要命名空间,因此我们在app.js中定义它,这是我们的主控制器:

var app = {
  view: {},
  controller: {},
  model: {},
  ui: {}
}

Within the folders we place single JavaScript files for each component. 在文件夹中,我们为每个组件放置单个JavaScript文件。 For this we could either use a lightweight JavaScript OOP library, such as MooTools or Prototype or define simple JS functions as our objects. 为此,我们可以使用轻量级的JavaScript OOP库,例如MooTools或Prototype,或者将简单的JS函数定义为我们的对象。 If you also want to inherit from parent classes, a library definitely makes sense. 如果您还想从父类继承,那么库肯定是有意义的。

Examples: 例子:

# Resources/controller/MyController.js
app.controller.MyController = function() {
   return {
      getView: function() {
         return new app.view.MyView().getView();
      }
   }
}

# Resources/view/MyView.js
app.view.MyView = function() {
   return {
      getView: function() {
         return Ti.UI.createWindow({...});
      }
   }
}

# Resources/view/MyModel.js
app.model.MyModel = function() {
   return {
      some: "data",
      foo: "bar"
   }
}

After that we can include all needed model/view/controller classes with Ti.include() in the app.js file and reference the components with our namespace: 之后,我们可以在app.js文件中包含所有需要的模型/视图/控制器类和Ti.include(),并使用我们的命名空间引用组件:

Ti.include("controller/MyController.js");
Ti.include("view/MyView.js");
var myController = new app.controller.MyController();
var myView = myController.getView();
myView.open();

The MVC approach would now presume that the controller "controls" the state of the view and passes data from the model into the view. MVC方法现在假定控制器“控制”视图的状态并将数据从模型传递到视图中。 The view consists only of UI elements and properties for styling. 视图仅包含UI元素和样式属性。 Any action which is made in the UI fires an event, which tells the controller to perform the desired action. 在UI中进行的任何操作都会触发一个事件,该事件告诉控制器执行所需的操作。

But of course, the exact definition of MVC might be different according to your personal taste ;) 但是,当然,根据您的个人品味,MVC的确切定义可能会有所不同;)

这也可能有所帮助:如何组织Titanium移动项目的基本结构: https//github.com/krawaller/Struct

Allow me to update this question since most of the responses are superseded. 请允许我更新此问题,因为大部分回复已被取代。 In Q4 2012, Appcelerator released the Alloy MVC (beta) Framework along with the latest IDE and SDK release, Titanium Studio 3.0 and SDK 3.0. 在2012年第四季度,Appcelerator发布了Alloy MVC(beta)框架以及最新的IDE和SDK版本,Titanium Studio 3.0和SDK 3.0。 Alloy is completely integrated with Studio, so it's quite easy to get a basic app running in less than 15 mins. Alloy与Studio完全集成,因此在不到15分钟的时间内运行基本应用程序非常容易。 Alloy introduces a significant folder restructure: The /app folder is now where all the development code resides. Alloy引入了重要的文件夹重构: / app文件夹现在是所有开发代码所在的位置。

The /Resources folder, where code used to reside, is now the updated equivalent of the /build folder. / Resources文件夹(用于驻留的代码)现在是/ build文件夹的更新等效项。 Compiled code in /Resources is overwritten at each build. 每个构建都会覆盖/ Resources中的编译代码。

I created a short introductory primer (screencast) on creating an Alloy project. 我创建了一个关于创建Alloy项目的简短介绍性入门(截屏视频)。 You can view it via my dropbox folder. 您可以通过我的Dropbox文件夹查看它。

Create Alloy Project 创建合金项目

It looks like Appcelerator made their own Appcelerator MVC in the marketplace I have not evaluated this yet. 看起来Appcelerator在市场上制作了自己的Appcelerator MVC我尚未对此进行评估。

More Info: http://johnkalberer.com/2011/09/29/appcelerator-mvc-example/ 更多信息: http//johnkalberer.com/2011/09/29/appcelerator-mvc-example/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM