這裡就不多說了,應該很容易就可以看懂了!這裡沒有用TextArea而改用了DataGrid! :-) 剛學會!
import mx.remoting.Service;
import mx.services.Log;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.debug.NetDebug;
import mx.controls.Alert; //加入Alert,以便彈出一個提示信息
//Initialize a Logger
mx.remoting.debug.NetDebug.initialize(); // initialize the NCD
var myLogger:Log = new Log( Log.DEBUG, "logger1" );
// override the default log handler
myLogger.onLog = function( message:String ):Void {
trace( "myLogger-->>>"+message );
}
//設定全局Service
_global.svc = new Service(
"http://localhost/amfphp/gateway.php",
myLogger,
"favorite",
null,
null);
/**
* 設定添加按鈕的動作
*/
append_btn.onRelease = function(){
var pc:PendingCall = svc.append(webname_txt.text,url_txt.text); //呼叫php中的append方法
pc.responder = new RelayResponder(this, "append_Result", "onError");
webname_txt.text = url_txt.text = ""; //清空文本框
}
append_Result = function(re:ResultEvent){ //添加按鈕響應結果
var id = re.result;
if(id != "error"){
mx.controls.Alert.show ("保存:"+id+" ->成功", "Debug Message", Alert.YES, _root, false, "prueba", Alert.OK);
refresh_data(); //刷新數據
}
else {
mx.controls.Alert.show ("技術原因未成保存", "Debug Message", Alert.YES, _root, false, "prueba", Alert.OK);
}
}
/***
* 刷新按鈕
*/
refresh_btn.onRelease = function(){
favorite_dg.removeAllColumns();
refresh_data();
}
/**
* 刷新數據
*/
function refresh_data(){
var pc:PendingCall = svc.get_data();
pc.responder = new RelayResponder(this, "getData_Result", "onError" );
}
/***
* 接收數據顯示
*/
function getData_Result(re: ResultEvent){
var rs = re.result;
favorite_dg.columnNames = ["id_PK", "webname", "url", "timeline"];
favorite_dg.dataProvider = rs;
/* 遍歷方法
f = rs.length; //數庫集大小
for(var i=0; i<f; i++){
favorite_dg.addItem({id_PK:rs.getItemAt(i).id_PK, webname:rs.getItemAt(i).webname, url:rs.getItemAt(i).url, timeline:rs.getItemAt(i).timeline});
}
*/
}
/***
* 出錯 :-(
*/
function onError(rs: FaultEvent){
mx.remoting.debug.NetDebug.trace({level:"None", message:"There was a problem: " + fault.fault.faultstring });
}
refresh_data(); //初始數據
stop();