【发布时间】:2011-11-16 09:07:21
【问题描述】:
我有一个 remoteobject 类来处理与我的远程数据服务的所有交互。每次进行调用时,数据服务都会检查用户是否具有有效会话。如果他们不这样做,它将不会运行请求的方法并返回失败。我可以在故障处理程序中捕获此失败。如果发生这种情况,我想做的是将登录屏幕推送给用户。
我已经尝试了以下
var navigator:ViewNavigator;
navigator.activeView.navigator.pushView(views.LoginScreen);
但这不起作用并且失败,无法访问空对象引用的属性或方法。这是有道理的。所以我的问题是如何获取对当前正在运行的视图导航器对象的引用并推送视图?
谢谢
这里要求的是完整的远程对象类
包远程处理 { 导入事件.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)));
}
}
}
}
【问题讨论】:
-
我们需要更多代码来了解您想要访问的内容。
-
代码添加到主帖。让我知道您是否还有其他需要查看的内容。如果凭据错误,我只想将用户发送到登录屏幕。
-
您希望您的导航器实例是什么?您不能以这种方式声明某些东西并认为它在您的应用程序中是全局的。我想您的代码中还有另一个地方声明了导航器,我错了吗?您必须区分处理应用程序外观的显示组件和处理数据读取的服务(例如)。
-
我认为这是问题的症结所在。我不知道如何获得它的参考。我不知道 viewnavigator 是在哪里声明的,我假设它是作为 ViewNavigatorApplication 的一部分创建的。我不太关心我是怎么做的。我只想能够从类中推送视图,以便我可以处理缺少身份验证的问题。我可以调度一个我在我的视图中监听的事件,但这意味着对于所有视图我都必须包含相同的代码。我希望集中处理异常。
-
你不知道 viewNavigator 是在哪里创建的?你在哪里创建你的视图?你能张贴你的项目文件的截图吗?不创建视图就无法创建应用程序!!!