我有一个remoteobject类,用于处理与远程数据服务的所有交互。每次进行调用时,dataservice都会检查用户是否具有有效会话。如果他们不这样做,它将不会运行所请求的方法,并返回失败。我可以在错误处理程序中捕获此失败。如果发生这种情况我想做的就是将登录屏幕推送给用户。
我试过以下
var navigator:ViewNavigator;
navigator.activeView.navigator.pushView(views.LoginScreen);
但是这不起作用并且无法访问无法访问null对象引用的属性或方法。这是有道理的。所以我的问题是如何获得对当前运行的视图导航器对象的引用并推送视图?
谢谢
这里要求的是完整的远程对象类
包远程 { import events.RemoteExceptionEvent;
import flash.events.*;
import mx.managers.CursorManager;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.events.FaultEvent;
import mx.rpc.remoting.RemoteObject;
import spark.components.ViewNavigator;
import views.FirstTime.ValidateUser;
/**
* Super class for all remote services that contains some generic methods.
*/
public class RemoteService extends EventDispatcher
{
private static var REMOTE_EXCEPTION:String = "Remote exception";
private static var NO_MESSAGE:String = "10001";
protected var remoteObject:RemoteObject;
private var amfChannelSet:ChannelSet;
/**
* Constructor accepting an id and destination for the actual RemoteObject to create. An event listener
* is added for exceptions.
* @param id String representing the id of the new RemoteObject to create
* @param destination String representing the destination of the RemoteObject to create
* @param amfChannelId String representing the Channel of the RemoteObject to create
* @param amfChannelEndpoint String representing the Endpoint URI of the RemoteObject to create
*/
public function RemoteService( serviceId:String
, serviceDestination:String
, amfChannelId:String
, amfChannelEndpoint:String
)
{
// Create a runtime Channelset for given Channel ID and Endpoinr URI
var amfChannel:AMFChannel = new AMFChannel(amfChannelId, amfChannelEndpoint);
amfChannelSet = new ChannelSet();
amfChannelSet.addChannel(amfChannel);
// Create the remoteObject instance
this.remoteObject = new RemoteObject(serviceId);
this.remoteObject.channelSet = amfChannelSet;
this.remoteObject.destination = serviceDestination;
this.remoteObject.addEventListener(FaultEvent.FAULT,onRemoteException);
this.remoteObject.setCredentials('test','test');
}
/**
* generic fault event handler for all remote object actions. based on the received message/code an action
* is taken, mostly throwing a new event.
* @param event FaultEvent received for handling
*/
public function onRemoteException(event:FaultEvent):void {
trace('code : ' + event.fault.faultCode +
', message : ' + event.fault.faultString +
',detail : ' + event.fault.faultDetail);
trace('fodun: ' + event.fault.faultDetail.indexOf("Authentication"));
if (event.fault.faultDetail.indexOf("Authentication") > 0)
{
var navigator:ViewNavigator;
navigator.activeView.navigator.pushView(views.LoginScreen);
}
else if (event.fault.faultString == REMOTE_EXCEPTION) {
EventDispatcher(
new RemoteExceptionEvent(RemoteExceptionEvent.REMOTE_EXCEPTION,
"unknown problem occurred during a remote call : " + event.fault.message));
} else if (event.fault.faultCode == NO_MESSAGE) {
EventDispatcher(
new RemoteExceptionEvent(RemoteExceptionEvent.REMOTE_EXCEPTION,
event.fault.faultString));
} else {
EventDispatcher((
new RemoteExceptionEvent(RemoteExceptionEvent.REMOTE_EXCEPTION,
"unknown runtime problem occurred during a remote call : " + event.fault.message)));
}
}
}
}
答案 0 :(得分:0)
从RemoteService类中访问您的视图或任何可视组件不是处理MVC应用程序的好方法。您要做的是在RemoteService类中调度一个基本上说“身份验证失败”的事件。然后,在您的视图中,您将收听此事件并将其解释为需要显示登录屏幕。见?