分类
Flash

基于MVC模式的Flash Remoting类

为方便自己,我特意用MVC模式改写了Flash Remoting类,适用于AS2.0。但愿对大家还有些用。
一、RemotingModel 父类

**
*   @class RemotingModel
*   @author Flying
*   @copyright 2006 riafan
*/
import mx.remoting.Service;
import mx.services.Log;
import mx.remoting.PendingCall;;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.debug.NetDebug;
import mx.events.EventDispatcher;

class com.riafan.RemotingModel {
private var gatewayURL:String;
private var servicePath:String;
private var svc:Service;
public var addEventListener:Function;
private var removeEventListener:Function;
private var dispatchEvent:Function;
private var dispatchQueue:Function;

/**
* Constructor
*
*   @param      $gatewayURL gateway URL
*   @param      $serviceName    service name
*   @param      $uid        username
*   @param      $pwd        password
*
*/
public function RemotingModel($gatewayURL, $serviceName, $uid, $pwd)
{
// initialize as a broadcaster
EventDispatcher.initialize(this);

// create a new service
svc = new Service ($gatewayURL, null, $serviceName, null, null);

// credentials
if ($uid != undefined && $pwd != undefined) {
svc.connection.setCredentials($uid, $pwd);
}
}
/**
* Global fault event
*/
private function handleRemotingModelError(fault:FaultEvent):Void
{
mx.remoting.debug.NetDebug.trace({level:"None", message:"Error: " + fault.fault.faultstring });
}
/**
* Event dispatcher
*
*   @param      $data       data
*   @param      $type       event type
*
*/
private function dispatch($data, $type)
{
// broadcast message
var eventObj:Object={target:this,type:$type}
eventObj.data = $data;
dispatchEvent(eventObj);
}

二、RemotingView父类

*   @class RemotingView
*   @author Flying
*   @copyright 2006 riafan
*/
import mx.events.EventDispatcher;
class com.riafan.RemotingView
{
private var target:MovieClip;
public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
public var dispatchQueue:Function;
/**
* Constructor
*
*   @param  mc  target (target timeline: _root || a mc)
*/
public function RemotingView ($target:MovieClip)
{
target = $target;

// initialize as a broadcaster
EventDispatcher.initialize(this);
}
/**
* Event dispatcher
*
*   @param      $data       data
*   @param      $type       event type
*
*/
private function dispatch($data, $type)
{
// broadcast message
var eventObj:Object={target:this,type:$type}
eventObj.data = $data;
dispatchEvent(eventObj);
}
}

实际应用中,如果我们的项目并不复杂,就没有必要写父类了。