I have a remoteobject class that handles all interactive with my remote data service. each time a call is made the dataservice checks that the user has a valid session. if they don’t it won’t run the requested method and it returns a fail. I can capture this fail in the fault handler. What I want to do it if it happens is push the login screen to the user.
I have tried the following
var navigator:ViewNavigator;
navigator.activeView.navigator.pushView(views.LoginScreen);
But this is not working and fails with Cannot access a property or method of a null object reference. which makes sense. so my question is how can I get a reference to the current running view navigator object and push the view?
Thanks
As requested here is the full remote object class
package remoting
{
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)));
}
}
}
}
Accessing your view or whatever visual components from within a RemoteService class is not a good way to deal with MVC applications. What you want to do is to dispatch an event in your RemoteService class that basically says “authentication has failed”. Then, in your view, you’ll listen for this event and interpret it as a need to display the login screen. See ?