利用Cairngorm框架开发flex应用

研究了近两天的Cairngorm框架开发的例子,感觉网上的例子好多是用RemoteObject发请求,但我需要的是httpservice发请求,在网上找了好多资料、试验,最终成功。

工程名:ownerarea
一、主Main: ownerarea.mxml(主要内容)
1、其中包含名为BaseDataPanel的component,add进来的
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:business="com.ownerarea.business.*"
xmlns:basedataControl="com.ownerarea.control.basedata.*">

<business:Service id="getBaseDataListService"/>
<basedataControl:BaseDataControl id="baseDataControl"/>

</mx:Application>


二、BaseDataPanel.mxml
....
//派发事件,此项目中一个动作为一个事件,每个事件类中自己维护event的type
internal function init(): void
{
var event: GetBaseDataListEvent = new GetBaseDataListEvent();
CairngormEventDispatcher.getInstance().dispatchEvent(event);
}
....


三、BaseDataControl类, 继承FrontController
//这里放事件常量,并利用基类监听事件
public static const GETDATALIST: String = "getDataList";

public function BaseDataControl()
{
initialiseCommands();
}

public function initialiseCommands() : void{
addCommand(BaseDataControl.GETDATALIST, GetDataListCommand);
}

四、GetBaseDataListEvent类,  继承CairngormEvent
//这里只完成一件事,利用基类绑定常量事件派发出去superpublic function GetBaseDataListEvent()
{
super(BaseDataControl.GETDATALIST);
}

五、service.mxml
//这里可以注入多个httpservice,注意要给ID,目的Delegate类调用
//如果需要传参数,在Delegate类写,这里只提供httpservice的url即可。
<?xml version="1.0" encoding="utf-8"?>
<cairngorm:ServiceLocator xmlns:mx="http://www.adobe.com/2006/mxml"
                          xmlns:cairngorm="com.adobe.cairngorm.business.*">

    <mx:HTTPService id="getBaseDataListService"
    url="http://localhost:8888/ownerarea/basedataList"
    method="POST"
    showBusyCursor="true" resultFormat="xml"/>

</cairngorm:ServiceLocator>

六、GetDataListDelegate类
//传参数时用 this.service.send({type:"1"}),如果需要传多个用逗号分隔,如
{type:"1",id:"1"}

public class GetDataListDelegate
{
private var responder : IResponder;
private var service: HTTPService;

public function GetDataListDelegate(responder: IResponder)
{
this.service = ServiceLocator.getInstance().getHTTPService("getBaseDataListService");
this.responder = responder;
}

public function getList(): void{
var token: AsyncToken = this.service.send({type:"1"});
//目的:指明此service的返回去哪里执行
token.addResponder(this.responder);
}

}

七、GetDataListCommand类, 实现 Command, IResponder
//注意,这里IResponder 为mx.rpc.IResponder,并不是cairngorm的,目的是为了注册service的Respond
//这个类调用Delegate完成数据请求,并处理请求成功与失败的结果
//结果需要这样调(data as ResultEvent).result.toString()

public function execute(event:CairngormEvent):void
{
var delegate: GetDataListDelegate = new GetDataListDelegate(this);
delegate.getList();
}

public function result(data:Object):void
{
trace("result:"+(data as ResultEvent).result.toString());


}

public function fault(info:Object):void
{
}

你可能感兴趣的:(框架,xml,Flex)