我看到了异步命令的好处(等待服务器响应...)但是在我的flex应用程序中它给我带来的问题比什么都重要。这就是我想要的:
每个命令只在前一个命令返回后(到结果或故障函数)执行
我想尽可能轻松地做到这一点..在执行长命令时,GUI必须变得无法响应(可能是等待消息)的方式(我可以在执行函数中显示等待消息,在故障或结果函数中删除它。)
任何想法?
答案 0 :(得分:0)
我通过扩展CairngormEvent类并添加两个属性来完成此任务:
package control.events
{
import com.adobe.cairngorm.control.CairngormEvent;
public class AbstractCairngormEvent extends CairngormEvent
{
public var successHandler:Function;
public var failureHandler:Function;
public function AbstractCairngormEvent(type:String)
{
super(type);
}
}
}
创建一个新的Class作为所有Commands的基础,它实现了ICommand:
package control.commands
{
import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;
import control.events.AbstractCairngormEvent;
/* This class is a base for all Commands. It allows the user to set callback
functions for successful completion and/or failure of the Command logic (i.e.
a WebService call). */
public class CairngormCommand implements ICommand
{
private var successHandler:Function;
private var failureHandler:Function;
public function execute(event:CairngormEvent):void
{
if (event is AbstractCairngormEvent)
{
var commandEvent:AbstractCairngormEvent =
event as AbstractCairngormEvent;
successHandler = commandEvent.successHandler;
failureHandler = commandEvent.failureHandler;
}
}
public function notifyCallerOfSuccess():void
{
if (successHandler != null)
successHandler.call(this);
}
public function notifyCallerOfFailure():void
{
if (failureHandler != null)
failureHandler.call(this);
}
}
}
然后在每个Command Class中,当必要的逻辑完成,或者出现错误/失败时,我调用CairngormCommand基类中的相应函数。这是一个例子:
// Something like this would be in your view:
private function callSomeWebService():void {
var event:WebServiceEvent = new WebServiceEvent();
myEvent.successHandler = callMyEventSuccessHandler;
myEvent.failureHandler = callMyEventFailureHandler;
}
private function callMyEventSuccessHandler():void {
Alert.show("SUCCESS!!!");
}
private function callMyEventFailureHandler():void {
Alert.show("FAILURE!!!");
}
// Here is the Event in your Controller:
package control.events
{
import control.events.AbstractCairngormEvent;
public class WebServiceEvent extends AbstractCairngormEvent
{
public static var EVENT_ID:String = "webService";
public function WebServiceEvent()
{
super(EVENT_ID);
}
}
}
// And here is the Command in your Controller:
package control.commands
{
import com.adobe.cairngorm.control.CairngormEvent;
import control.commands.CairngormCommand;
import control.events.WebServiceEvent;
public class WebServiceCommand extends CairngormCommand
{
override public function execute(event:CairngormEvent):void
{
super.execute(event);
... // Call WebServices
}
...
private function webServiceResultHandler():void
{
// Handle results
...
notifyCallerOfSuccess();
}
private function webServiceFaultHandler():void
{
// Handle fault
...
notifyCallerOfFailure();
}
}
}
我现在在我的Cairngorm应用程序中使用此独占权。最好的部分是,如果您不需要成功和/或失败的回调,您可以将这些属性保留在事件实例化之外,只需调度事件即可。
在加载显示之前加载显示所需数据的示例:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
usePreloader="false" initialize="initializeHandler();">
<mx:Script>
<![CDATA[
import control.events.InitializeApplicationEvent;
private function initializeHandler():void
{
initializeApplication();
}
private function initializeApplication():void
{
var event:InitializeApplicationEvent =
new InitializeApplicationEvent();
event.successHandler = initializationSuccessHandler;
event.dispatch();
}
private function initializationSuccessHandler():void
{
applicationContainer.visible = true;
}
]]>
</mx:Script>
<control:Services xmlns:control="control.*" />
<control:Controller xmlns:control="control.*" />
<view:ApplicationContainer xmlns:view="view.*" id="applicationContainer"
width="100%" height="100%" visible="false" />
</mx:Application>
InitializeApplicationCommand(注意如何根据需要多次链接事件和调用者):
package control.commands
{
import com.adobe.cairngorm.control.CairngormEvent;
import control.events.GetEvenMoreDataEvent;
import control.events.GetSomeDataEvent;
import control.events.GetSomeMoreDataEvent;
import control.events.InitializeApplicationEvent;
public class InitializeApplicationCommand extends CairngormCommand
{
override public function execute(event:CairngormEvent):void
{
super.execute(event);
getSomeData();
}
private function getSomeData():void
{
var event:GetSomeDataEvent = new GetSomeDataEvent();
event.successHandler = getSomeMoreData;
event.failureHandler = errorHandler;
event.dispatch();
}
private function getSomeMoreData():void
{
var event:GetSomeMoreDataEvent = new GetSomeMoreDataEvent();
event.successHandler = getEvenMoreData;
event.failureHandler = errorHandler;
event.dispatch();
}
private function getEvenMoreData():void
{
var event:GetEvenMoreDataEvent = new GetEvenMoreDataEvent();
event.successHandler = notifyCallerOfSuccess;
event.failureHandler = errorHandler;
event.dispatch();
}
private function errorHandler():void
{
alert.show("error");
}
}
}