我看到了异步命令的好处(等待服务器响应......),但在我的 flex 应用程序中,它给我带来的问题比任何事情都多。这就是我想要的:
每个命令仅在前一个返回后执行(结果或故障函数)
而且我想尽可能轻松地做到这一点..顺便说一下,在执行长命令时,GUI必须变得无响应(可能是等待消息)(我可以在执行函数中显示等待消息并将其删除)故障或结果函数..)
任何的想法?
我看到了异步命令的好处(等待服务器响应......),但在我的 flex 应用程序中,它给我带来的问题比任何事情都多。这就是我想要的:
每个命令仅在前一个返回后执行(结果或故障函数)
而且我想尽可能轻松地做到这一点..顺便说一下,在执行长命令时,GUI必须变得无响应(可能是等待消息)(我可以在执行函数中显示等待消息并将其删除)故障或结果函数..)
任何的想法?
我通过扩展 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);
}
}
}
并创建一个新类作为所有命令的基础,它实现了 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");
}
}
}