生活随笔
收集整理的這篇文章主要介紹了
pureMVC简单示例及其原理讲解四(Controller层)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本節將講述pureMVC示例中的Controller層。
Controller層有以下文件組成:
- AddUserCommand.as
- DeleteUserCommand.as
- ModelPrepCommand.as
- ViewPrepCommand.as
- StartupCommand.as
AddUserCommand?。顧名思義,它是添加用戶命令。讓我們首先看看代碼。
Addusercommand.as代碼??
package?com.superwulei.controller??{??????import?com.superwulei.model.UserProxy;??????import?com.superwulei.model.vo.UserVO;????????????import?mx.controls.Alert;????????????import?org.puremvc.as3.interfaces.INotification;??????import?org.puremvc.as3.patterns.command.SimpleCommand;????????public?class?AddUserCommand?extends?SimpleCommand??????{??????????override?public?function?execute(notification:INotification):void??????????{????????????????????????????var?user:UserVO?=?notification.getBody()?as?UserVO;??????????????var?userProxy:UserProxy?=?facade.retrieveProxy(UserProxy.NAME)?as?UserProxy;????????????????????????????if(user.isValid){??????????????????userProxy.addItem(user);??????????????}else{??????????????????Alert.show("請檢查用戶名和密碼");??????????????}??????????}????????????????}??}?? ?AddUserCommand是一個單一命令(SimpleCommand),自定義SimpleCommand必須繼承SimpleCommand并重寫execute方法。execute方法表示這個命令的執行。曾經在上一篇《pureMVC簡單示例及其原理講解——View層?》中提到的添加用戶的邏輯代碼,應該在這里編寫。還記得上一篇中提到的“View層本身不處理各種操作,但是發送通知”么?
上一篇中發送通知的代碼 sendNotification(ApplicationFacade.USER_ADD,userForm.user);
?拿出這段代碼是特意的想說明AddUserCommand的execute方法中的notification.getBody()其實就是userForm.user,嚴謹的說應該是userFrom.user作為參數傳到execute方法中來。如此我們在這里通過userProxy.addItem(user)就實現了用戶的添加。userProxy中的users就多了一個user。
?
DeleteUserCommand?,刪除用戶命令。代碼如下,與添加用戶道理一樣,不多言。
Deleteusercommand deleteusercommand.as代碼??
package?com.superwulei.controller??{??????import?com.superwulei.model.UserProxy;??????import?com.superwulei.model.vo.UserVO;????????????import?org.puremvc.as3.interfaces.INotification;??????import?org.puremvc.as3.patterns.command.SimpleCommand;????????public?class?DeleteUserCommand?extends?SimpleCommand??????{??????????override?public?function?execute(notification:INotification):void??????????{??????????????var?user:UserVO?=?notification.getBody()?as?UserVO;??????????????var?userProxy:UserProxy?=?facade.retrieveProxy(UserProxy.NAME)?as?UserProxy;??????????????userProxy.deleteItem(user);??????????}????????????????}??}?? ?ModelPrepCommand、ViewPrepCommand分別是Model層注冊和View層注冊。說道注冊就要道一道。在pureMVC中,一切總控制是facade,因此無論是Proxy、Mediator還是Command都要在facade中注冊。上面四個Command全部為SimpleCommand,最后一個StartupCommand為MacroCommand(復合命令)。StartupCommand包含了多個SimpleCommand,通過addSubCommand方法添加了子命令,并在之后在facade上注冊了AddUserCommand和DeleteUserCommand。
Modelprepcommand.as代碼??
package?com.superwulei.controller??{??????import?com.superwulei.model.UserProxy;????????????import?org.puremvc.as3.interfaces.INotification;??????import?org.puremvc.as3.patterns.command.SimpleCommand;????????public?class?ModelPrepCommand?extends?SimpleCommand??????{??????????override?public?function?execute(notification:INotification):void??????????{??????????????/*?注冊Model?*/??????????????facade.registerProxy(new?UserProxy());??????????}??????}??}?? ?
Viewprepcommand.as代碼??
package?com.superwulei.controller??{??????import?com.superwulei.view.UserFormMediator;??????import?com.superwulei.view.UserListMediator;????????????import?org.puremvc.as3.interfaces.INotification;??????import?org.puremvc.as3.patterns.command.SimpleCommand;????????public?class?ViewPrepCommand?extends?SimpleCommand??????{??????????override?public?function?execute(notification:INotification):void??????????{??????????????var?app:MyPureMVCdemo?=?notification.getBody()?as?MyPureMVCdemo;??????????????/*?注冊View?*/??????????????facade.registerMediator(new?UserFormMediator(app.userForm));??????????????facade.registerMediator(new?UserListMediator(app.userList));??????????}??????}??}?? ?
Startupcommand.as代碼??
package?com.superwulei.controller??{??????import?com.superwulei.ApplicationFacade;????????????import?org.puremvc.as3.patterns.command.MacroCommand;????????public?class?StartupCommand?extends?MacroCommand??????{??????????override?protected?function?initializeMacroCommand():void{??????????????addSubCommand(ModelPrepCommand);??????????????addSubCommand(ViewPrepCommand);??????????????/*?注冊添加、刪除用戶命令?*/??????????????facade.registerCommand(ApplicationFacade.USER_ADD,AddUserCommand);??????????????facade.registerCommand(ApplicationFacade.USER_DELETE,DeleteUserCommand);??????????}??????}??}?? ?通過使用facade的registerCommand就好象添加一個監聽器一樣,當有sendNotification發送出來的時候,就會有對應的Command的execute方法被執行。
Controller層包含的應該是整個應用程序的邏輯業務。
轉載于:https://www.cnblogs.com/fuland/p/3632847.html
總結
以上是生活随笔為你收集整理的pureMVC简单示例及其原理讲解四(Controller层)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。