?? listtopicproxy.as
字號(hào):
package cn.riahome.guestbook.puremvc.model
{
import cn.riahome.guestbook.puremvc.ApplicationFacade;
import cn.riahome.guestbook.puremvc.model.vo.TopicVO;
import flash.net.URLVariables;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.messaging.messages.HTTPRequestMessage;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;
import org.puremvc.as3.interfaces.IProxy;
import org.puremvc.as3.patterns.proxy.Proxy;
public class ListTopicProxy extends Proxy implements IProxy
{
/**
*
* 目前分析途徑: Main.mxml -> ApplicationFacade.as -> StartupCommand.as -> ListTopicProxy.as
* 完整分析途徑: Main.mxml -> ApplicationFacade.as -> StartupCommand.as -> ListTopicProxy.as -> ListPanelMediator.as
*
* 為這個(gè) Proxy(這里為L(zhǎng)istTopicProxy) 起一個(gè)名字. 這個(gè)名字是唯一的, 且應(yīng)該定義為靜態(tài)的常量.
**/
public static const NAME:String = "ListTopicProxy";
private var httpService:HTTPService;
public function ListTopicProxy()
{
/**
* 每一個(gè) Proxy 都有一個(gè) data 屬性, 用來(lái)存儲(chǔ)需要傳遞的數(shù)據(jù), 就是用來(lái)攜帶數(shù)據(jù)的.
* 這里必須先調(diào)用父類的構(gòu)造函數(shù), 把自己的名字和攜帶的 數(shù)據(jù)/數(shù)據(jù)類型 傳進(jìn)父類的構(gòu)造函數(shù)
* 下面?zhèn)鬟f的第二個(gè)參數(shù)是一個(gè) ArrayCollection, 當(dāng)執(zhí)行 super() 后, data(Object類型)屬性就會(huì)變?yōu)橐粋€(gè) ArrayCollection.
**/
super(NAME, new ArrayCollection() );
/**
* 在每個(gè) Proxy 里面, 一定要做的事情就是上面的兩項(xiàng): 為自己起一個(gè)名字 以及 先調(diào)用父類的構(gòu)造函數(shù)
* 然后具體要做的事情, 就根據(jù)需要而定了.
* 這里要做的事是: 從服務(wù)端獲取留言數(shù)據(jù), 當(dāng)獲取數(shù)據(jù)完成后, 就發(fā)布一個(gè)通知來(lái)告訴大家:"我完成了向服務(wù)端獲取留言數(shù)據(jù)了,我擁有這些數(shù)據(jù)".
* 您看看下面的 onResult() 函數(shù), 該函數(shù)里有一條語(yǔ)句: sendNotification( ApplicationFacade.GET_ALL_TOPIC_COMPLETE, data ).
* 第一個(gè)參數(shù)就是通知消息, 以靜態(tài)常量的方式在 ApplicationFacade 類里定義了.
**/
httpService = new HTTPService();
httpService.method = mx.messaging.messages.HTTPRequestMessage.GET_METHOD;
httpService.resultFormat = HTTPService.RESULT_FORMAT_XML;
httpService.url = "php/listTopic.php"; // 這里的 url 根據(jù)您的虛擬目錄不同而不同
httpService.addEventListener( ResultEvent.RESULT, onResult );
httpService.addEventListener( FaultEvent.FAULT, onFault);
}
private function onResult( event:ResultEvent ):void
{
var arr:ArrayCollection = new ArrayCollection();
var result:XMLList = XML( event.result ).children();
for( var i:uint = 0; i < result.children().length(); i++)
{
var o:TopicVO = new TopicVO( result[i].@id, result[i].@addTime, result[i].@username, result[i] );
arr.addItem( o );
}
data = arr;
/**
* 通知: 各單位注意, 我完成了向服務(wù)端獲取留言數(shù)據(jù)了,我擁有這些數(shù)據(jù), 有意者請(qǐng)接收通知!
* 在這個(gè)例子里, 希望得到這些數(shù)據(jù)的當(dāng)然是 ListPanel.mxml 了.
* 但, 之前已經(jīng)說(shuō)過(guò), 取得數(shù)據(jù)這類的工作并不用 UI(也說(shuō)是component) 來(lái)完成, 這些工作由 UI 的中介器(Mediator)來(lái)完成.
* 在這里, ListPanel.mxml 的中介器是 ListPanelMediator.
* 現(xiàn)在, 請(qǐng)您打開 ListPanelMediator.as 文件, 或者在 "分析途經(jīng)" 路線上退回上一級(jí)(也就是 StartupCommand.as 文件),
* 然后找到 facade.registerMediator( new ListPanelMediator( app.listPanel ) ) 這一句,
* 按著 Ctrl 鍵點(diǎn)擊 ListPanelMediator, 進(jìn)入去看看源碼.
*
* 目前 分析途經(jīng): Main.mxml -> ApplicationFacade.as -> StartupCommand.as -> ListTopicProxy.as
*
**/
sendNotification( ApplicationFacade.GET_ALL_TOPIC_COMPLETE, data );
}
private function onFault( event:FaultEvent ):void
{
Alert.show( event.message.toString(), "提示");
}
public function getAllTopic():void
{
httpService.send( new URLVariables("ran="+Math.random()) ); // 給一個(gè)隨機(jī)參數(shù), 避免緩存
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -