Titanium的MVC框架"Alloy"的介绍

Alloy是Appcelerator为Titanium开发的一款MVC框架,旨在提高开发效率和代码可维护性。该框架支持通过XML定义UI布局,并利用JSON进行样式设置。Alloy可通过npm安装,并集成到现有Titanium项目中。
[b]Alloy[/b](合金)是Appcelerator公司为Titanium开发的一个新的框架,采用MVC结构,内部代码编号“ZipTi”。从整体上看更类似于Ruby on Rails(代码构成,命令行操作等)。

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

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

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

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

也可以先克隆到本地,然后再安装
[quote]git clone https://github.com/appcelerator/alloy.git[/quote]
[quote][sudo] npm install -g .[/quote]

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

[quote]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[/quote]

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

[b]【目录构成】[/b]
[list][*]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.[/list]
[img]http://dl.iteye.com/upload/attachment/0070/9511/b621edc1-e8e7-3eca-b191-308caf3b13bc.gif[/img]

[b]主要的几个文件:[/b]

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

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


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

(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">

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

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

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

详细可以参考[url=https://github.com/appcelerator/alloy]https://github.com/appcelerator/alloy[/url]

官方发布的MVC框架,构成上是否合理也有待于广大Ti开发者的验证。不过如果布局采用XML定义的话,那么可视化开发工具将不会太遥远了!。
【SCI复现】含可再生能源与储能的区域微电网最优运行:应对不确定性的解鲁棒性与非预见性研究(Matlab代码实现)内容概要:本文围绕含可再生能源与储能的区域微电网最优运行展开研究,重点探讨应对不确定性的解鲁棒性与非预见性策略,通过Matlab代码实现SCI论文复现。研究涵盖多阶段鲁棒调度模型、机会约束规划、需求响应机制及储能系统优化配置,结合风电、光伏等可再生能源出力的不确定性建模,提出兼顾系统经济性与鲁棒性的优化运行方案。文中详细展示了模型构建、算法设计(如C&CG算法、大M法)及仿真验证全过程,适用于微电网能量管理、电力系统优化调度等领域的科研与工程实践。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事微电网、能源管理相关工作的工程技术人员。; 使用场景及目标:①复现SCI级微电网鲁棒优化研究成果,掌握应对风光负荷不确定性的建模与求解方法;②深入理解两阶段鲁棒优化、分布鲁棒优化、机会约束规划等先进优化方法在能源系统中的实际应用;③为撰写高水平学术论文或开展相关课题研究提供代码参考和技术支持。; 阅读建议:建议读者结合文档提供的Matlab代码逐模块学习,重点关注不确定性建模、鲁棒优化模型构建与求解流程,并尝试在不同场景下调试与扩展代码,以深化对微电网优化运行机制的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值