[转载]Flex框架Cairngorm2 SequenceCommand用法封装 -Mikel

[转载]Flex框架Cairngorm2 SequenceCommand用法封装

2011年6月21日 分类: 开发笔记, Flash

[转载]Flex框架Cairngorm2 SequenceCommand用法封装 – God bless you – 博客园.

概述:

The SequenceCommand is provided as a “psuedo-abstract” (since ActionScript has no real concept of abstract classes) base-class that can be extended when you wish to chain commands together for a single user-gesture, or establish some simple form of decision-based workflow.

By extending SequenceCommand, you can specify the event that should be broadcast to the controller (causing another command execution without a further user-gesture) when the current command has completed execution.

For a command implementing the Responder interface, you may choose to sequence a subsequent command on successful completion of the command, in the onResult() handler, or on failure of the command in the onFault() method.

For commands that do not implement the Responder interface, you can simply chain commands by causing the sequenced command to be invoked as the last action in your command’s execute() method.

一般用法:

1. SequenceCommand实例构造函数中设置nextEvent

2. 对于实现IResponder的Command,在onResult()或onFault()中调用executeNextCommand()来执行下一chain command

3. 对于未实现IResponder的Command,在execute()方法的最后调用executeNextCommand()来执行下一chain command

对于复杂链式命令,封装ChainEvent后的实现:

1. 定义一个ChainEvent,所有需要按顺序执行的事件都继承自该事件。

package xxx.events
{
import com.adobe.cairngorm.control.CairngormEvent;
import flash.events.Event;
/**
*
* ChainEvent,所有需要按顺序执行的事件都继承自该事件
*
* @see com.adobe.cairngorm.control.CairngormEvent
*
*/
public class ChainEvent extends CairngormEvent
{
public static const CHAIN_EVENT:String = “xxx.events.ChainEvent”;
public var nextEvent:ChainEvent;
public var myValue:String;     //测试值
public function ChainEvent()
{
super( CHAIN_EVENT );
}
public override function clone():Event
{
var res:ChainEvent = new ChainEvent();
res.nextEvent = nextEvent;
res.myValue = myValue;
return res;
}
}
}

2. 建立ChainEvent工厂类

package xxx.events.utils
{
import xxx.events.ChainEvent;

/**
*
* ChainEventFactory,chainEvent静态工厂类
*
*/
public final class ChainEventFactory
{
public static function createChainEvent(evts:Array):ChainEvent
{
if ( evts.length < 1 )
return null;
var res:ChainEvent = evts[ 0 ] as ChainEvent;
for (var i:int=0; i<evts.length; i++ )
{
if (i<evts.length-1)
{
var event:ChainEvent = evts[ i ] as ChainEvent;
var nextEvent:ChainEvent = evts[ i+1 ] as ChainEvent;
event.nextEvent = nextEvent;
}
}
return res;
}
}
}

3. 实现Command

package xxx.command
{
import com.adobe.cairngorm.commands.SequenceCommand;
import com.adobe.cairngorm.control.CairngormEvent;
import xxx.events.ChainEvent;

public final class ChainCommand extends SequenceCommand
{
public override function execute(event:CairngormEvent):void
{
var chainEvent:ChainEvent = event as ChainEvent;
trace(chainEvent.myValue);
if (chainEvent.nextEvent != null)
{
this.nextEvent = chainEvent.nextEvent;
this.executeNextCommand();
}
}
}
}

4. 链式ChainEvent派发

var firstChainEvent:ChainEvent = new ChainEvent();
var secondChainEvent:ChainEvent = new ChainEvent();
var thirdChainEvent:ChainEvent = new ChainEvent();

firstChainEvent.myValue = “firstChainEvent”;
secondChainEvent.myValue = “secondChainEvent”;
thirdChainEvent.myValue = “thirdChainEvent”;

var chainEvent:ChainEvent = ChainEventFactory.createChainEvent([firstChainEvent,secondChainEvent,thirdChainEvent]);
chainEvent.dispatch();

最后结果输出:

firstChainEvent

secondChainEvent

thirdChainEvent


标签: , ,
本文的评论功能被关闭了.
备案信息冀ICP 0007948