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

PureMVC for Unity

幾個月前改用 Unity 開發遊戲,到目前的心得為:組件式開發真的是很便利,但是當組件數量多到一定程度時,結構上就有點可怕,常常在某 GameObject 上掛了組件後就忘了它的存在,雖然可以使用 Singleton design pattern 來製作主要的 Manager(本人對 Singleton 並不是很熱愛),程式還是會亂到一定程度,搜尋了一些 Unity with MVC 討論,一部分的人都對實作 MVC 不是很熱絡,也許是 Unity 特有的開發環境導致。 以前開發 Adobe Flex 專案最愛用的 MVC Framework 就是 PureMVC,即使後來有更方便的 MVC Framework 的也擋不住我對它的熱愛。Unity 是沒有所謂的全域 Root Scene,所有場景都是獨立,想要將 AS3 實作邏輯套用在 Unity 上將控制項都在 PureMVC 架構中實作是有點矯情多餘。如何保持 Unity 組件開發模式,導入 PureMVC 鬆綁主要邏輯,就是這次實驗的重點。 不清楚 PureMVC 的朋友們可以到這邊參觀一下: PureMVC 我也會 PureMVC C# Standard Framework on GitHub ViewComponent 與 Mediator 整合是首要工作: 由於 Unity 沒有全域 Root Scene,如果將 new Mediator( viewComponent ) 寫在 PureMVC 架構下,即使透過 GameObject.Find 找那個對應的 GameObject 就轉了九彎十八拐,寫起來一點都不愉快,尤其考慮到場景的轉換,兩個場景中相關 Mediator 的註冊與移除處理,何況對 Unity 組件來說,能不能被打包動態載入是件重要的事。綜合以上問題點,反向思考,改由 GameObject 掛載中介組件,在 OnEnable 與 OnDisable 通知 Facade 去註冊與移除其 Mediator,一來簡化為了實作 Meditaor 掛載 ViewComponent 而對 static class GameObject 的依賴,二來也不會對 Unity 組件開發模式有太大的影響。

[Swift3] weak 與 unowned 關鍵字

雖然在 Swift 中看起來"很像"是不需要煩惱內存管理的問題,不過實際上它還是遵循著自動引用計數 (ARC) 的規則,當一個物件沒有被其他對象引用時會自動被銷毀,如果三魂七魄沒有完全回位的話,就會有個靈體留在現世的空間裡,最經典的範例如下: 閉包(Closure)引用 classClassA { typealias Complete = ()->() var name : String var onComplete : Complete? init(_ name: String){ self.name = name print("Hello I am \(self.name)") onComplete = { print("\(self.name): onComplete!") // --> 閉包引用 self, 計數 + 1 } } deinit { print("deinit: \(self.name)") } } var a : ClassA? = ClassA("A") // --> 引用計數 + 1 a = nil // 2-1 = 1 還剩下 1 所以沒辦法銷毀 ---output------- Hello I am A 由於這邊的 onComplete 宣告為 Optional, 正確的做法要連同 onComplete 一起刪除才可以被回收,若不是 Optional 則會進入無法回收狀態: var b : ClassA? = ClassA("B") b?.onComplete = nil // --> 還好是 Optional 可以設成 nil 計數 - 1 b = nil // 計數 = 0 所以被回收 ---output------- Hello I am B deinit: B 但是做人不需要煩惱太多,這時候就出動 unowned 關鍵字讓物件可以順利被回收: onComplete = { [unowned self] in print...