關於 Puremvc Command 的那點事

4,466 views
4,357 views

Published on

PureMVC Command 相關應用
http://erinylin.blogspot.com/
勘誤:
Page20 為 : 啥叫非同步 Command?

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,466
On SlideShare
0
From Embeds
0
Number of Embeds
2,385
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

關於 Puremvc Command 的那點事

  1. 1. 關於 PureMVC Command 的 那點事 Erin Lin http://about.me/erinlin
  2. 2. 到處都有 Command...
  3. 3. Command 是?
  4. 4. Command 是?
  5. 5. Command 是?
  6. 6. Command 是?
  7. 7. CommandCommand CommandCommand Command Command Command 不想學還是要學的 Command 命令 設計模式 Command Command CommandCommand Command Command Command
  8. 8. <<interface>> ICommand Controller execute():void addCommand() removeCommand()//------------------- Command doSomething() execute():void
  9. 9. 真情推薦傻瓜都可以看懂的設計模式入門書 自己上網去買! http://www.oreilly.com.tw/product_java.php?id=a163
  10. 10. 回到 PureMVC...
  11. 11. PureMVC兩個基本必知 Command
  12. 12. 最基本的SimpleCommand
  13. 13. 反正要亂搞就是 extendsSimpleCommand
  14. 14. 你認識的第一支繼承 SimpleCommand 寫法package com.mvc.controls{ import org.puremvc.as3.interfaces.INotification; import org.puremvc.as3.patterns.command.SimpleCommand; public class StartupCommand extends SimpleCommand { public function StartupCommand() { super(); } override public function execute(notification:INotification):void{ //初始 Application 要做的事情 //facade.registerMediator, facade.registerProxy // or facade.registerCommand //通常都會將 Application 傳進來做應用 } }}
  15. 15. 群組同時執行的MacroCommand
  16. 16. 使用 MacroCommand 的 StartupCommandpackage com.mvc.controls{ import org.puremvc.as3.patterns.command.MacroCommand; public class StartupCommand extends MacroCommand { public function StartupCommand() { super(); } override protected function initializeMacroCommand() :void { addSubCommand( ModelPrepCommand ); addSubCommand( ViewPrepCommand ); addSubCommand( 你寫的Command ); } }}
  17. 17. 啊...我想要一個命令做完, 才要執行下一個....最後還要來個完美的 Ending 要怎麼辦?
  18. 18. http://trac.puremvc.org/PureMVC_AS3/ PureMVC Utilities 使用的時候要心存感激喔!
  19. 19. 處理非同步的 AsyncCommand http://trac.puremvc.org/Utility_AS3_AsyncCommand
  20. 20. 啥叫非同步?
  21. 21. 當然就是一件工作做完 才做下一個指令 照順序來不懂嗎?
  22. 22. AsyncCommand 也有 兩個 Class 給你用
  23. 23. AsyncCommand andAsyncMacroCommand
  24. 24. 基本用法是以成組的方式應用
  25. 25. package controllers{ import flash.utils.setTimeout; import org.puremvc.as3.multicore.interfaces.ICommand; import org.puremvc.as3.multicore.interfaces.INotification; import org.puremvc.as3.multicore.patterns.command.AsyncCommand; public class AsyncCommand0 extends AsyncCommand implements ICommand { public function AsyncCommand0() { super(); } override public function execute(notification:INotification):void{ trace("lalala AsyncCommand0"); setTimeout( commandComplete, 1000); } }}
  26. 26. package controllers{ importorg.puremvc.as3.multicore.patterns.command.AsyncMacroCommand; public class StartupCommand extends AsyncMacroCommand { public function StartupCommand() { super(); } private function onComplete():void{ trace("end of StartupCommand"); } override protected function initializeAsyncMacroCommand():void{ setOnComplete( onComplete ); addSubCommand( AsyncCommand0 ); addSubCommand( AsyncCommand1 ); addSubCommand( AsyncCommand2 ); } }}
  27. 27. AsyncCommand DEMO
  28. 28. 所以 Command 可以做什麼?
  29. 29. 應用一:Assets loader
  30. 30. package controllers{ import mx.rpc.AsyncToken; import mx.rpc.IResponder; import mx.rpc.http.HTTPService; import org.puremvc.as3.multicore.interfaces.ICommand; import org.puremvc.as3.multicore.interfaces.INotification; import org.puremvc.as3.multicore.patterns.command.AsyncCommand; public class LoadConfigCommand extends AsyncCommand implements IResponder { public function LoadConfigCommand() { super(); } override public function execute(notification:INotification):void{ var service:HTTPService = new HTTPService; service.resultFormat = xml; service.url = "your configuration files url"; service.send(); } public function result( result:Object ):void{ this.commandComplete(); } public function fault( result:Object ):void{ //如果要中斷流程,需要在這邊傳出 ERROR notification 由其他 Command 處理 } }
  31. 31. package controllers{ import org.puremvc.as3.multicore.patterns.command.AsyncMacroCommand; public class StartupCommand extends AsyncMacroCommand { public function StartupCommand() { super(); } private function onComplete():void{ trace("end of StartupCommand"); sendNotification( "APP_INIT" ); } override protected function initializeAsyncMacroCommand():void{ this.setOnComplete( onComplete ); addSubCommand( LoadConfigCommand ); addSubCommand( LoadAssetsCommand ); addSubCommand( LoadXXXCommand ); } }}
  32. 32. 應用二:做外掛...
  33. 33. package com.controls{ public class GroupEditorCommand extends SimpleCommand implements ICommand { public function GroupEditorCommand() { super(); } override public function execute(notification:INotification):void { switch( notification.getName() ){ case "GroupEditorCommand.INIT": //將之前開發用的 proxy notification 組織起來 facade.registerCommand( "DataProxy.ITEM_UPDATED" , GroupEditorCommand ); facade.registerCommand( "XXXProxy.NOTIFICATION_NAME" , GroupEditorCommand ); showLoader(); //看你要做什麼起始 break; case "DataProxy.ITEM_UPDATED": //看要叫 proxy 做啥,還是 call 啥畫面出來 break; } } private function clearCommands():void{ facade.removeCommand( "DataProxy.ITEM_UPDATED" ); facade.removeCommand( "XXXProxy.NOTIFICATION_NAME" ); removeLoader(); sendNotification( "GroupEditorCommand.CLOSE" ); } private function showLoader( string:String ):void{ //將檔畫面的 loader call 到前景 } private function removeLoader():void{ //remove loader } }}
  34. 34. 其他?
  35. 35. 其實你要怎樣玩它 就開心的玩吧!想太多就什麼都寫不出來了!
  36. 36. 最後...
  37. 37. 請保持愉快的心情開心的寫程式吧!
  38. 38. FIN
  39. 39. 參考資料• http://trac.puremvc.org/PureMVC_AS3/• http://www.oreilly.com.tw/product_java.php?id=a163• http://trac.puremvc.org/Utility_AS3_AsyncCommand

×