Skip to main content

Titanium APP with PureMVC & CoffeeScript


昨天一時無聊研究起最近很紅的 CoffeeScript,小試結果不禁有種相見很晚的 fu....Jeremy Ashkenas 你實在是太棒啦!!!!!


先前使用 Javascript 來寫 Titanium APP 的時候最大的問題還是來自於 Javascript 是個易學難精的語言,debug 不易,怎麼寫好像都沒問題但是跑出來的結果就是很神奇, Class 的實作也是百百種....雖然先前 文章分享的:PureMVC for Titanium Mobile 簡單實作了 PureMVC 供 Titanium 使用,不過實際操作時還是會覺得有點彆扭。

CoffeeScript 講求的就是採用 Javascript the Good parts,在不影響效能的轉換下,保證轉出來的 js 都是優良的,更重要的是它的類別實作....各位朋友們啊!這個才叫類別啊!!!(淚奔)

秉持著哪裡都要用 PureMVC, 即使改用 CoffeeScript 也不例外
以下原始碼皆可以在本人的 github 找到。

在這邊貼一段 Titanium APP with PureMVC & CoffeeScript 的實作:
為了怕 混亂 Global scope, Puremvc 還是包覆在 Puremvc 的 namespace 下

#引入 puremvc
Ti.include "puremvc-coffee-1.0.js"

trace = (s)->
Ti.API.info s

trace "Hello coffeescript with puremvc."

#利用工廠方法建立 MainWindow 方法
MainWindow = ->
win = Ti.UI.createWindow
title: 'Tab 1'
backgroundColor: '#fff'

label = Ti.UI.createLabel
color: '#999'
text: 'Hello Coffeescript & Puremvc'
font:
fontSize: '20dp'
fontFamily: 'Helvetica Neue'
textAlign: 'center'
width: 'auto'

win.add label;
win

# MainWindow 用的 Mediator
class MainMediator extends Puremvc.Mediator
listNotificationInterests: -> ["Hello"]

handleNotification:(note)->
trace 'MainMediator got "Hello": ' + note

onRegister:->
trace 'Mediator onRegister.:' + @getMediatorName()
# get view component
@getViewComponent().open()

#主要由 StartupCommand 註冊 MainMediator
class StartupCommand extends Puremvc.SimpleCommand
execute:(note)->
trace 'startupCommand executed!!!'
@facade.registerMediator new MainMediator 'MainMediator', new MainWindow()
# to MainMediator
@sendNotification "Hello"

最後:

setTimeout( ->
Puremvc.facade.registerCommand 'startup', StartupCommand
Puremvc.facade.registerProxy new DemoProxy 'DemoProxy'
Puremvc.facade.sendNotification 'startup'
200)


Edit:
為了怕大家不清楚如何在 Titanium Studio 中使用 CoffeeScript ,在這邊提供一個中文說明:請參考高見龍的 在Titanium Studio使用CoffeeScript來寫app

Edit2:
關於如何在 Titanium 中使用 CoffeeScript 也可以直接安裝好 CoffeeScript 後,直接利用 Titanium 下拉是功能表 / Commands / CoffeeScript Bundle 執行 Compile and Display JS,不需要真的安裝 Titanium plug-in

Comments

Popular posts from this blog

[Flex] PureMVC standard with Spring extensions

由於上次稍微玩了一下 Robotlegs 依賴注入(DI) 主導的 MVC 框架,而著名也使用依賴注入的 Java / Java EE 的 Spring framework 出了 for ActionScript 的版本,剛好在最近 Spring ActionScript 1.0 正式 release 了(想了解 Spring 是啥咪東東的話請自行找 google 大神),這個版本除了基本框架外,也包含了 Cairngorm 與 PureMVC 的外掛...想當然耳,就拿來測試一下用在 PureMVC 內的感覺囉!! 參考了 官方範例 中 PureMVC 唯二的範例原始檔,以下使用的是「設定檔依賴注入 facade 透過 addConfigSource() 的方式來 init 」:(其實除了 embed 外,都是外部載入) Online Demo with source code 工作環境:FlashBuilder, Flex SDK4 請下載 PureMVC Standard 版本 再下載 Spring ActionScript 最新版本後,除了 spring-actionscript-cairngorm 不需要外,都放到 /src 下(記得只需要 org 開始...),也別忘了lib 內的 swc 檔 copy 到 /libs 下 Spring 的 injection 並不像 Robotlegs 直接來個 [Inject] metadata 的自動化那樣方便,但是其冷血度(檔案的鬆偶程度)更勝後者!如果你要使用設定檔(applicationContext.xml) 來做注入的話,準備工作就挺多的...XD 依照 applicationContext.xml 內設定的方式分別寫入 constructor 或者是 setter 依賴注入(本範例統一使用 setter injection) 為了跟大家都沒關係所以都使用 interface 來處理,所以你會在範例中發現大家都有介面...(並沒有真的研究過 Spring,也許還有其他作法) 準備 compiler 時候要用的 classe。由於在 setter, getter 的寫法上都使用 interface,所以真正用到的 class 需要預先在輸出階段就打包到程式內。 基本上 PureMVC 類 class...

[543] 最近的 Erin 在做什麼?

最近 Erin 噗浪玩很大,都忘了要寫 blog... 自從四月初開始認真當 SOHO 後,每天都過著"醉生夢死"的生活...?? 目前的行程: 五六月 - 滿檔中... 七月( 每週一三晚間 ) - 授課 飛肯 ActionScript 3.0 & XML 資料庫整合應用班 (會準備什麼隱藏課程?你來了就知道...XD ) 七八九月 - 短期約聘面談中 十月 - 等你聯絡囉~~^^ 近期如果你有"不急的" Flex / AIR / Flash / Flash lite 相關的案子需要外發都可以直接與我聯絡!也接受短期約聘喔! 1 ) 如何聯絡 Erin? 寄 e-mail 是最快的喔!不然可以透過 blog 上的 Plugoo 視窗與我聯絡,再來就是噗浪...XD E-mail: erinylin [at] gmail [dot] com Plurk: http://www.plurk.com/erinlin 2 ) 為什麼不公開留 MSN...?? 嗯...因為 Erin 記憶不好,外加 MSN 一堆幾百年沒有聯絡的朋友名單...所以還是先透過 e-mail 聯絡一下再加入比較記得... 3 ) Erin 到底會什麼? 啊...這個問題就有點難回答了,Erin 古早以前是個美術設計師,也當過電腦兼職講師,後來轉職當互動工程師,現在主要是做 AS1/AS2/AS3 "都可以"的前端互動開發(程式為主)。凡舉 Flex / AIR / Flash / Flash lite 相關的案子都做過,整合過很多專案,動畫也製作過(人物設定+動畫製作),在手機產業界接觸了 UI 設計與 UE 研究,最近經手的案子有國外活動網站(AS3+pv3D)中文化,公司進銷存系統網路化(Flex)...所以 Erin 到底能做什麼?嗯...就等你來研究囉~~ 4 ) 為什麼沒有作品集可以看? 這個就要問問為什麼一堆公司都要你簽 NDA (保密協定) ,搞的 Erin 也懶得公開經手過的作品,所以想要看到 Erin 做過什麼,就請記得找 Erin 面談時要註明『請帶電腦』這四個字 =) 裏 1 ) 目前 Erin 缺什麼? 嗯...缺男人(咦?這才是這篇的重點嗎?)