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上下文的加载:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300"
  5. creationComplete="init(event)"
  6. viewSourceURL="srcview/index.html">
  7. <fx:Script>
  8. <![CDATA[
  9. import mx.events.FlexEvent;
  10.  
  11. import org.springextensions.actionscript.context.support.XMLApplicationContext;
  12. import org.springextensions.actionscript.core.task.xml.TaskNamespaceHandler;
  13. import org.springextensions.actionscript.ioc.factory.xml.UtilNamespaceHandler;
  14.  
  15. import utils.ClassRef;
  16.  
  17. public var applicationContext:XMLApplicationContext = new XMLApplicationContext("config/application_context.xml");
  18.  
  19. protected function init(event:FlexEvent):void
  20. {
  21. ClassRef.regist();
  22. applicationContext.addNamespaceHandler(new UtilNamespaceHandler());
  23. applicationContext.addNamespaceHandler(new TaskNamespaceHandler());
  24. applicationContext.addEventListener(Event.COMPLETE,completeHandler);
  25. applicationContext.load();
  26. }
  27.  
  28. protected function completeHandler(event:Event):void {
  29. trace("fine");
  30. }
  31.  
  32. ]]>
  33. </fx:Script>
  34. <fx:Declarations>
  35. <!-- Place non-visual elements (e.g., services, value objects) here -->
  36. </fx:Declarations>
  37. </s:Application>

数据Model的接口设计:

  1. package model
  2. {
  3. import mx.collections.ArrayCollection;
  4.  
  5. import vo.UserVO;
  6.  
  7. [Bindable]public interface IApplicationModel
  8. {
  9. function get employeeList():ArrayCollection;
  10. function set employeeList(value:ArrayCollection):void;
  11. function get user():UserVO;
  12. function set user(value:UserVO):void;
  13. function get viewIndex():int;
  14. function set viewIndex(value:int):void;
  15. }
  16. }

Model我们将通过XML配置中各个实例的依赖注入,在实例中获得引用。然后我们实现两个操作,以便在Service中使用。

操作一:登录,注意我通过setTimeout虚拟了后台响应的延时。

  1. package service.operation
  2. {
  3. import flash.utils.setTimeout;
  4.  
  5. import mx.managers.CursorManager;
  6.  
  7. import org.springextensions.actionscript.core.operation.AbstractOperation;
  8.  
  9. import vo.UserVO;
  10.  
  11. public class LoginOperation extends AbstractOperation
  12. {
  13. public function LoginOperation(user:UserVO)
  14. {
  15. super();
  16. CursorManager.setBusyCursor();
  17. setTimeout(success,1000,user);
  18. }
  19.  
  20. private function success(user:UserVO):void {
  21. if(user.userName == "jim" && user.password == "jim") {
  22. result = 1;
  23. } else {
  24. result = 0;
  25. }
  26. CursorManager.removeBusyCursor();
  27. dispatchCompleteEvent();
  28. }
  29.  
  30. }
  31. }

操作二:获取用户信息列表

  1. package service.operation
  2. {
  3. import flash.utils.setTimeout;
  4.  
  5. import mx.collections.ArrayCollection;
  6. import mx.managers.CursorManager;
  7.  
  8. import org.springextensions.actionscript.core.operation.AbstractOperation;
  9.  
  10. import vo.UserVO;
  11.  
  12. public class GetEmployeeListOperation extends AbstractOperation
  13. {
  14. public function GetEmployeeListOperation()
  15. {
  16. super();
  17. CursorManager.setBusyCursor();
  18. setTimeout(success,1000);
  19. }
  20.  
  21. private function success():void {
  22. CursorManager.removeBusyCursor();
  23. var data:Array = [];
  24. data.push(new UserVO("Jim","xx"));
  25. data.push(new UserVO("Tom","xx"));
  26. data.push(new UserVO("Marry","xx"));
  27. result = new ArrayCollection(data);
  28. dispatchCompleteEvent();
  29. }
  30.  
  31. }

然后实现服务类,这里只有两个方法:登录和获取数据。注意上述的两个操作在这里得到应用。

  1. package service
  2. {
  3. import org.springextensions.actionscript.core.operation.IOperation;
  4.  
  5. import service.operation.GetEmployeeListOperation;
  6. import service.operation.LoginOperation;
  7.  
  8. import vo.UserVO;
  9.  
  10. public class UserService implements IUserService
  11. {
  12. public function UserService()
  13. {
  14. }
  15.  
  16. public function login(user:UserVO):IOperation
  17. {
  18. return new LoginOperation(user);
  19. }
  20.  
  21. public function getEmployeeList():IOperation
  22. {
  23. return new GetEmployeeListOperation();
  24. }
  25. }
  26. }

然后我们实现两个命令(Command),来调取服务。

命令一:登录

  1. package command
  2. {
  3. import model.IApplicationModel;
  4.  
  5. import org.springextensions.actionscript.core.command.IAsyncCommand;
  6. import org.springextensions.actionscript.core.operation.AbstractOperation;
  7. import org.springextensions.actionscript.core.operation.IOperation;
  8. import org.springextensions.actionscript.core.operation.OperationEvent;
  9.  
  10. import service.IUserService;
  11.  
  12. public class LoginCommand extends AbstractOperation implements IAsyncCommand
  13. {
  14. private var _userService:IUserService;
  15. private var _applicationModel:IApplicationModel;
  16.  
  17.  
  18. public function LoginCommand(userService:IUserService, applicationModel:IApplicationModel)
  19. {
  20. super();
  21. _userService = userService;
  22. _applicationModel = applicationModel;
  23. }
  24.  
  25. public function execute():*
  26. {
  27. var operation:IOperation = _userService.login(_applicationModel.user);
  28. operation.addCompleteListener(handleComplete);
  29. operation.addErrorListener(handleError);
  30. }
  31.  
  32. protected function handleComplete(event:OperationEvent):void {
  33. trace(event.result);
  34. if(event.result == 1) {
  35. _applicationModel.user.loginStatus = true;
  36. }
  37. dispatchCompleteEvent(event.result);
  38. }
  39.  
  40. protected function handleError(event:OperationEvent):void {
  41. dispatchErrorEvent(event.error);
  42. }
  43.  
  44.  
  45. }
  46. }

命令二:获取用户数据

  1. package command
  2. {
  3. import model.IApplicationModel;
  4.  
  5. import org.springextensions.actionscript.core.command.IAsyncCommand;
  6. import org.springextensions.actionscript.core.operation.AbstractOperation;
  7. import org.springextensions.actionscript.core.operation.IOperation;
  8. import org.springextensions.actionscript.core.operation.OperationEvent;
  9.  
  10. import service.IUserService;
  11.  
  12. public class GetEmployeeListCommand extends AbstractOperation implements IAsyncCommand
  13. {
  14. private var _userService:IUserService;
  15. private var _applicationModel:IApplicationModel;
  16.  
  17.  
  18. public function GetEmployeeListCommand(userService:IUserService, applicationModel:IApplicationModel)
  19. {
  20. super();
  21. _userService = userService;
  22. _applicationModel = applicationModel;
  23. }
  24.  
  25. public function execute():*
  26. {
  27. var operation:IOperation = _userService.getEmployeeList();
  28. operation.addCompleteListener(handleComplete);
  29. operation.addErrorListener(handleError);
  30. }
  31.  
  32. protected function handleComplete(event:OperationEvent):void {
  33. _applicationModel.viewIndex = 1;
  34. _applicationModel.employeeList = event.result;
  35. dispatchCompleteEvent(event.result);
  36. }
  37.  
  38. protected function handleError(event:OperationEvent):void {
  39. dispatchErrorEvent(event.error);
  40. }
  41.  
  42.  
  43. }
  44. }

各个部分准备完毕之后,我们在XML配置中完成装配:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <objects xmlns="http://www.springactionscript.org/schema/objects"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:util="http://www.springactionscript.org/schema/util"
  5. xmlns:t="http://www.springactionscript.org/schema/task"
  6. >
  7.  
  8. <object id="appModel" class="model.ModelLocator" factory-method="getInstance"/>
  9. <object id="appService" class="service.UserService" />
  10. <object id="loginCommand" scope="prototype" class="command.LoginCommand">
  11. <constructor-arg ref="appService" />
  12. <constructor-arg ref="appModel" />
  13. </object>
  14. <object id="getListCommand" scope="prototype" class="command.GetEmployeeListCommand">
  15. <constructor-arg ref="appService" />
  16. <constructor-arg ref="appModel" />
  17. </object>
  18. <object id="faultCommand" scope="prototype" class="command.LoginFaultCommand" />
  19.  
  20. <t:task id="initTask" scope="prototype">
  21. <t:and command="loginCommand"/>
  22. <t:next command="getListCommand"/>
  23. </t:task>
  24.  
  25. <object id="mainView" class="view.MainView">
  26. <property name="appModel" ref="appModel" />
  27. <property name="initTask" ref="initTask" />
  28. </object>
  29.  
  30. <object class="utils.ApplicationViewAssembler" init-method="init">
  31. <property name="elements">
  32. <value>
  33. <array>
  34. <ref>mainView</ref>
  35. </array>
  36. </value>
  37. </property>
  38. </object>
  39.  
  40. </objects>

注意其中的initTask,包含了两个Command,而且是顺序执行的关系,先执行登录操作,然后获取用户列表数据。感兴趣的朋友可以尝试修改和扩展,变为加入If判断或别的机制的应用配置。这是一个简单的示例,真实的应用场景会比它要复杂,欢迎大家在这里分享自己的经验。

riadevID: 
您给予的分值: None 平均分: 10 (1 vote)

不错的文章

不错的文章

发表新评论

  • 网页地址和电子邮件地址将会被自动转换为链接。
  • 行和段被自动切分。
  • 您可以使用下面的标签来高亮显示您的评论内容: <code>, <blockcode>. 可以使用"[foo]".旁边显示标签样式 "<foo>" PHP代码可以用这样的区块来包含<?php ... ?> or <% ... %>

更多格式化选项信息

验证区域
系统验证:请回答下面的问题