`
iMzw
  • 浏览: 191091 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Flex之使用Cairngorm(3) - Command & Event

阅读更多
其实ActionScript3很象Java代码。Cairngorm也是大量借鉴了Java EE里的模式。

建立Command,实现com.adobe.cairngorm.commands.Command接口. 在execute(event:CairngormEvent) 里实现你的业务逻辑。
UserLoginCommand.as
package net.imzw.UserManagerDemo.command{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.controls.Alert;
	
	import net.imzw.UserManagerDemo.event.UserOperationEvent;
	import net.imzw.UserManagerDemo.model.UserManagerModelLocator;
	import net.imzw.UserManagerDemo.vo.UserVO;

	public class UserLoginCommand implements Command{
		private var modelLocator:UserManagerModelLocator = UserManagerModelLocator.getInstance();
		public function UserLoginCommand(){
			super();
		}
		public function execute(event:CairngormEvent):void{
			var userOPEvent:UserOperationEvent = event as UserOperationEvent;
			var user:UserVO = userOPEvent.user;
			
			if( user.loginName == "imzw" && user.password == "imzw" ){
				modelLocator.workflowState = UserManagerModelLocator.MAIN_SCREEN;
				modelLocator.currentUser = user;
			}else{
				Alert.show("LoginName or Password invalid!");
			}
		}
		
	}
}


建立Event, 继承com.adobe.cairngorm.control.CairngormEvent。
UserOperationEvent.as
package net.imzw.UserManagerDemo.event
{
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import flash.events.Event;
	
	import net.imzw.UserManagerDemo.vo.UserVO;
	
	public class UserOperationEvent extends CairngormEvent{
		public static const USER_OPERATION_EVENT:String = "userOperation";
		public var user:UserVO;
		
		public function UserOperationEvent(user:UserVO=null, bubbles:Boolean=false, cancelable:Boolean=false){
			this.user = user;
			super(USER_OPERATION_EVENT, bubbles, cancelable);
		}
		
		override public function clone():Event{
			return new UserOperationEvent(this.user);
		}
	}
}

建立Controller,通过addCommand 方法添加UserLoginCommand到Controller。
UserController.as
package net.imzw.UserManagerDemo.control{
	import com.adobe.cairngorm.control.FrontController;
	
	import net.imzw.UserManagerDemo.command.UserLoginCommand;
	import net.imzw.UserManagerDemo.event.UserOperationEvent;
	
	public class UserController extends FrontController	{
		public function UserController(){
			super();
			this.init();
		}
		
		private function init():void{
			this.addCommand(UserOperationEvent.USER_OPERATION_EVENT, UserLoginCommand);
		}
	}
}


实例化Controller到Application级。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
	xmlns:view="net.imzw.UserManagerDemo.views.*"
	xmlns:control="net.imzw.UserManagerDemo.control.*" 
	horizontalAlign="center" verticalAlign="middle"> 
	<mx:Script>
		<![CDATA[
			import net.imzw.UserManagerDemo.model.UserManagerModelLocator;
			import mx.controls.Alert;
			
			[Bindable]
			private var modelLocator:UserManagerModelLocator = UserManagerModelLocator.getInstance();
		]]>
	</mx:Script>
	
	<control:UserController />
	<mx:ViewStack width="400" height="300" 
		selectedIndex="{modelLocator.workflowState}">
		<view:LoginForm />
		<view:MainScreen />
	</mx:ViewStack>
</mx:Application>


代码见附件。
1
0
分享到:
评论
2 楼 iMzw 2008-08-14  
参见下面这篇文章的源码
http://nealmi.iteye.com/blog/212285
1 楼 wangwanttt 2008-08-14  
咋没delegate和server.mxml

相关推荐

Global site tag (gtag.js) - Google Analytics