Spring ActionScript Operation API入门教程[二]:实例
在上一篇文章中我们简单介绍了Spring ActionScript的Operation API,这里我们通过一个实例,来理解如何在我们的项目开发中引入这个机制。
这里假设项目需求是,需要用户先登录,然后进入到用户显示列表,这两个部分都需要调取后台服务进行异步操作,正好符合Operation API的应用范畴。
最后完成Demo的示意图:

点击这里查看完成Demo的演示:
http://www.riameeting.com/examples/OperationDemo/
在示例上点击右键可查看源码,也可以点击这里查看:
http://www.riameeting.com/examples/OperationDemo/srcview/index.html
这里假设您已经具备了Spring ActionScript的使用经验,不再详细阐述项目的构建步骤(如果您还不熟悉,请参阅这篇文章:Spring ActionScript入门教程(2)-一个简单的实例),而是对一些关键要点加以介绍:
项目主文件,我们依然分离了逻辑代码的执行,只保留了Spring ActionScript上下文的加载:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300" creationComplete="init(event)" viewSourceURL="srcview/index.html"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; import org.springextensions.actionscript.context.support.XMLApplicationContext; import org.springextensions.actionscript.core.task.xml.TaskNamespaceHandler; import org.springextensions.actionscript.ioc.factory.xml.UtilNamespaceHandler; import utils.ClassRef; public var applicationContext:XMLApplicationContext = new XMLApplicationContext("config/application_context.xml"); protected function init(event:FlexEvent):void { ClassRef.regist(); applicationContext.addNamespaceHandler(new UtilNamespaceHandler()); applicationContext.addNamespaceHandler(new TaskNamespaceHandler()); applicationContext.load(); } trace("fine"); } ]]> </fx:Script> <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> </s:Application>
数据Model的接口设计:
package model { import mx.collections.ArrayCollection; import vo.UserVO; [Bindable]public interface IApplicationModel { function get employeeList():ArrayCollection; function set employeeList(value:ArrayCollection):void; function get user():UserVO; function set user(value:UserVO):void; } }
Model我们将通过XML配置中各个实例的依赖注入,在实例中获得引用。然后我们实现两个操作,以便在Service中使用。
操作一:登录,注意我通过setTimeout虚拟了后台响应的延时。
package service.operation { import flash.utils.setTimeout; import mx.managers.CursorManager; import org.springextensions.actionscript.core.operation.AbstractOperation; import vo.UserVO; public class LoginOperation extends AbstractOperation { public function LoginOperation(user:UserVO) { super(); CursorManager.setBusyCursor(); setTimeout(success,1000,user); } private function success(user:UserVO):void { if(user.userName == "jim" && user.password == "jim") { result = 1; } else { result = 0; } CursorManager.removeBusyCursor(); dispatchCompleteEvent(); } } }
操作二:获取用户信息列表
package service.operation { import flash.utils.setTimeout; import mx.collections.ArrayCollection; import mx.managers.CursorManager; import org.springextensions.actionscript.core.operation.AbstractOperation; import vo.UserVO; public class GetEmployeeListOperation extends AbstractOperation { public function GetEmployeeListOperation() { super(); CursorManager.setBusyCursor(); setTimeout(success,1000); } private function success():void { CursorManager.removeBusyCursor(); data.push(new UserVO("Jim","xx")); data.push(new UserVO("Tom","xx")); data.push(new UserVO("Marry","xx")); result = new ArrayCollection(data); dispatchCompleteEvent(); } }
然后实现服务类,这里只有两个方法:登录和获取数据。注意上述的两个操作在这里得到应用。
package service { import org.springextensions.actionscript.core.operation.IOperation; import service.operation.GetEmployeeListOperation; import service.operation.LoginOperation; import vo.UserVO; public class UserService implements IUserService { public function UserService() { } public function login(user:UserVO):IOperation { return new LoginOperation(user); } public function getEmployeeList():IOperation { return new GetEmployeeListOperation(); } } }
然后我们实现两个命令(Command),来调取服务。
命令一:登录
package command { import model.IApplicationModel; import org.springextensions.actionscript.core.command.IAsyncCommand; import org.springextensions.actionscript.core.operation.AbstractOperation; import org.springextensions.actionscript.core.operation.IOperation; import org.springextensions.actionscript.core.operation.OperationEvent; import service.IUserService; public class LoginCommand extends AbstractOperation implements IAsyncCommand { private var _userService:IUserService; private var _applicationModel:IApplicationModel; public function LoginCommand(userService:IUserService, applicationModel:IApplicationModel) { super(); _userService = userService; _applicationModel = applicationModel; } public function execute():* { var operation:IOperation = _userService.login(_applicationModel.user); operation.addCompleteListener(handleComplete); operation.addErrorListener(handleError); } protected function handleComplete(event:OperationEvent):void { trace(event.result); if(event.result == 1) { _applicationModel.user.loginStatus = true; } dispatchCompleteEvent(event.result); } protected function handleError(event:OperationEvent):void { dispatchErrorEvent(event.error); } } }
命令二:获取用户数据
package command { import model.IApplicationModel; import org.springextensions.actionscript.core.command.IAsyncCommand; import org.springextensions.actionscript.core.operation.AbstractOperation; import org.springextensions.actionscript.core.operation.IOperation; import org.springextensions.actionscript.core.operation.OperationEvent; import service.IUserService; public class GetEmployeeListCommand extends AbstractOperation implements IAsyncCommand { private var _userService:IUserService; private var _applicationModel:IApplicationModel; public function GetEmployeeListCommand(userService:IUserService, applicationModel:IApplicationModel) { super(); _userService = userService; _applicationModel = applicationModel; } public function execute():* { var operation:IOperation = _userService.getEmployeeList(); operation.addCompleteListener(handleComplete); operation.addErrorListener(handleError); } protected function handleComplete(event:OperationEvent):void { _applicationModel.viewIndex = 1; _applicationModel.employeeList = event.result; dispatchCompleteEvent(event.result); } protected function handleError(event:OperationEvent):void { dispatchErrorEvent(event.error); } } }
各个部分准备完毕之后,我们在XML配置中完成装配:
<?xml version="1.0" encoding="utf-8"?> <objects xmlns="http://www.springactionscript.org/schema/objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springactionscript.org/schema/util" xmlns:t="http://www.springactionscript.org/schema/task" > <object id="appModel" class="model.ModelLocator" factory-method="getInstance"/> <object id="appService" class="service.UserService" /> <object id="loginCommand" scope="prototype" class="command.LoginCommand"> <constructor-arg ref="appService" /> <constructor-arg ref="appModel" /> </object> <object id="getListCommand" scope="prototype" class="command.GetEmployeeListCommand"> <constructor-arg ref="appService" /> <constructor-arg ref="appModel" /> </object> <object id="faultCommand" scope="prototype" class="command.LoginFaultCommand" /> <t:task id="initTask" scope="prototype"> <t:and command="loginCommand"/> <t:next command="getListCommand"/> </t:task> <object id="mainView" class="view.MainView"> <property name="appModel" ref="appModel" /> <property name="initTask" ref="initTask" /> </object> <object class="utils.ApplicationViewAssembler" init-method="init"> <property name="elements"> <value> <array> <ref>mainView</ref> </array> </value> </property> </object> </objects>
注意其中的initTask,包含了两个Command,而且是顺序执行的关系,先执行登录操作,然后获取用户列表数据。感兴趣的朋友可以尝试修改和扩展,变为加入If判断或别的机制的应用配置。这是一个简单的示例,真实的应用场景会比它要复杂,欢迎大家在这里分享自己的经验。

.gif)
.gif)




.gif)
不错的文章
不错的文章
发表新评论