`
rensanning
  • 浏览: 3514409 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37483
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604351
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:678121
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87298
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399823
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69078
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90494
社区版块
存档分类
最新评论

Titanium的MVC框架"Alloy"的介绍

阅读更多
Alloy(合金)是Appcelerator公司为Titanium开发的一个新的框架,采用MVC结构,内部代码编号“ZipTi”。从整体上看更类似于Ruby on Rails(代码构成,命令行操作等)。

源码依旧托管在GitHub:
https://github.com/appcelerator/alloy

【局限性】
(1)只能应用于OSX
(2)目前的状态是Unstable

【目的】
(1)提高开发效率Productivity
(2)提高可维护性Maintainability
(3)确保最佳实战Best Practices

【安装】
Alloy采用npm发布(前提是需要Node.JS NPM的环境)通过下面的命令来安装:
引用
[sudo] npm install -g alloy


也可以先克隆到本地,然后再安装
引用
git clone https://github.com/appcelerator/alloy.git

引用
[sudo] npm install -g .


【创建app】
先通过Titanium Studio,titanium.py,Titanium CLI创建一个项目,然后在控制台,进入项目的根目录输入以下命令:

引用
alloy new .
       .__  .__
_____  |  | |  |   ____ ___.__.
\__  \ |  | |  |  /  _ <   |  |
/ __ \|  |_|  |_(  <_> )___  |
(____  /____/____/\____// ____|
     \/                 \/
Alloy by Appcelerator. The MVC app framework for Titanium.
2012-07-18 13:44:20 -- [DEBUG] Creating directory: plugins
2012-07-18 13:44:20 -- [DEBUG] Creating directory: plugins/ti.alloy
2012-07-18 13:44:20 -- [INFO ] Deployed ti.alloy plugin to plugins/ti.alloy/plugin.py
2012-07-18 13:44:20 -- [INFO ] Installed 'ti.alloy' plugin to tiapp.xml
2012-07-18 13:44:20 -- [INFO ] Generated new project at: app


创建成功后,就会作成一个叫app的文件夹,其中包含了alloy app的骨架代码。

【目录构成】
  • views - this is where your views should go in the format view.xml
  • controllers - this is where your controllers should go in the format view.js.
  • styles - this is where your view styling logic should go in the format view.json.
  • models - this is where your model files will go.
  • assets - this is where you should put your image assets and other misc. files that you want copied into the Resources directory.
  • migrations - this is where your database migration files will be stored.
  • lib - this is where you should put application specific files, typically in the CommonJS format.
  • vendor - this is where you should put any vendor specific modules, typically in the CommonJS format. Do not place native modules in this folder.
  • config - Contains application specific config.


主要的几个文件:

(1)app/controllers/index.js
Controller :主要是事件处理,业务逻辑

$.t.on('click',function(e) {
    alert($.t.text);
});
$.index.open();


  • $ ----Alloy包装对象别名
  • $.t ---获取ID为"t"的对象
  • on("事件名", "回调函数") ---等价于addEventListener函数

(2)app/styles/index.json
Style:类似于CSS,设置UI的颜色,大小等

{
    ".container": {
        "backgroundColor":"white"
    },
    "Label": {
        "width": Ti.UI.SIZE,
        "height": Ti.UI.SIZE,
        "color": "#000"
    }
}


(3)app/views/index.xml
View:类似于HTML,设置UI布局

<Window class="container">
    <Label id="t">Hello, World</Label>
    <Button id="b">Click me</Button>
</Window>


采用XML定义页面UI控件以及从属关系
Window = Ti.UI.Window
Label = Ti.UI.Label
XML中定义的属性就是控件的初始参数值。

如果使用Ti.UI以外的View,比如MapView的话,使用一下方法:
<View ns="Ti.Map" id="map">

【开发】
可以通过Titanium Studio来开发,也可以使用控制台。

(1)Titanium Studio的话,通过plugins/ti.alloy来运行
(2)CLI的话
通过以下命令运行
引用
alloy compile
alloy run . iphone (目前只支持iphone)


通过命令行生成代码:
引用
alloy generate view <name>
alloy generate controller <name>
alloy generate model <name> [column_name:type, ...]
alloy generate migration <name>
alloy generate widget <name>


详细可以参考https://github.com/appcelerator/alloy

官方发布的MVC框架,构成上是否合理也有待于广大Ti开发者的验证。不过如果布局采用XML定义的话,那么可视化开发工具将不会太遥远了!。

  • 大小: 14.1 KB
1
0
分享到:
评论
1 楼 jsnjlc 2014-01-09  
刚开始玩Titanium,看了你的文章了解到Alloy到底是什么,谢谢了。

相关推荐

Global site tag (gtag.js) - Google Analytics