Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Pages: [1]
Print
Author Topic: Question about startup  (Read 9919 times)
skasssam
Jr. Member
**
Posts: 18


View Profile Email
« on: June 02, 2008, 05:31:45 »

I'm not sure if I am thinking about this correctly or approaching it the correct way but here is what I am trying to accomplish:

I am using AMFPHP, Flex and puremvc of course

I am implementing user authentication and want to emulate user sessions in flex

When a user logs in to the Flex application I set a session var for the user in AMFPHP. Then if the user reloads the page with the flex application I want to detect that the user is already authenticated and instead of taking them to the loginView stack I want them to go directly to the applicationView stack.

I have logging in and out functional.

I also have a button on my loginView that triggers the loginMediator to checkSession. Upon success the view stack is changed to the application, upon failue the view stack is changed to the login. This button works.

I have a sendNotification in my StartupCommand. This is suppose to send a notification to the loginMediator to checkSession which handles sending the notification to update the viewstack. But this notification never is received and I have put everything to handle the notification in loginMediator.

How can I make sure that checkSession is automatically called right after the application loads?

Any help is appreciated. TIA,
Shinan
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #1 on: June 02, 2008, 11:48:07 »

This notification you are sending from StartupCommand:

Is being sent after the succesful registration of the. LoginMediator. I have to ask, the same as the tech support guy at Dell has to ask if my computer is plugged in :)

Are you certain the mediator has registered the notification interest?

You are certain that the notification is never received because you've set a breakpoint on the switch statement in your handleNotification method and it wasn't triggered?

And you are using the latest version of the framework?

-=Cliff>
Logged
skasssam
Jr. Member
**
Posts: 18


View Profile Email
« Reply #2 on: June 02, 2008, 12:43:37 »

No those are valid questions and yes to all. Let me restate my question correctly.

checkSession does execute. The problem is that for some reason the first time checkSession runs loginVO is null. If I run checkSession twice, the second time it returns the loginVO which is the right behavior.

In loginMediator:
:
private function checkSession():void {
loginProxy = facade.retrieveProxy( LoginProxy.NAME ) as LoginProxy;
Alert.show(ObjectUtil.toString(loginProxy.loggedIn),"In checkSession");
if (loginProxy.loggedIn) {
sendNotification( ApplicationFacade.VIEW_FBAPP );
sendNotification ( ApplicationFacade.UPDATE_WELCOME,loginProxy.loginVO );
} else {
sendNotification( ApplicationFacade.VIEW_LOGIN );
}
}

In loginProxy:
:
public function get loggedIn():Boolean {
loginService.checkUserSession.addEventListener( ResultEvent.RESULT, onCheckUserSessionResult );
loginService.checkUserSession.send()
Alert.show(ObjectUtil.toString(loginVO),"In loggedIn");
return ( loginVO != null );
}
 
// To log out, simply clear the LoginVO and log out from the HTTP session for extra security
public function logout( ) : void {
setData(null);
loginService.userLogout.send();
}

private function onCheckUserSessionResult( event:ResultEvent ):void {
//Alert.show(ObjectUtil.toString(event.result),"In onCheckUserSessionResult");
if (!event.result) {
setData(null)
} else {
setData(event.result as LoginVO)

}
}

Thanks!
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #3 on: June 02, 2008, 01:56:23 »

:
public function get loggedIn():Boolean
{
loginService.checkUserSession.addEventListener( ResultEvent.RESULT, onCheckUserSessionResult );
loginService.checkUserSession.send()
Alert.show(ObjectUtil.toString(loginVO),"In loggedIn");
return ( loginVO != null );
}

This gives me the impression that you're expecting that the result of the async call to checkUserSession that you're invoking here is going to be available by the end of this method. It's not. onCheckUserSessionResult will not be called until later when the service returns, even though execution will continue locally in the client. That is to say, the call is not blocking.

What needs to happen is the startup registers the proxy and mediator, and the proxy automatically checks the session on registration, sending a notification to the mediator when the result comes back.

Try this in the LoginProxy:
:
// callled automatically when Proxy is registered
override public function onRegister():void
{
loginService.checkUserSession.addEventListener( ResultEvent.RESULT, onCheckUserSessionResult );
loginService.checkUserSession.send()
}

public function get loggedIn():Boolean
{
return (data != null);
}

private function onCheckUserSessionResult( event:ResultEvent ):void
{
if (!event.result) {
setData(null);
} else {
setData(event.result as LoginVO);
}
sendNotification(SESSION_STATUS,this.loggedIn);
}

Then make the mediator interested in the LoginProxy.SESSION_STATUS Notification, inspect the body and find a Boolean representing the status.

So now the Proxy is requesting session status as a matter of course at startup, and sending a notification when it receives the result, the mediator then acts on its interest in that result. The difference is that the mediator isn't requesting the status and expecting a synchronous result, which it can't get if that request is triggering an asynchronous request.

-=Cliff>
« Last Edit: August 03, 2008, 04:16:26 by puremvc » Logged
skasssam
Jr. Member
**
Posts: 18


View Profile Email
« Reply #4 on: June 03, 2008, 06:23:33 »

Got it!

Absolutely Brilliant Cliff. Thank you for the solution. You Rock!

P.S.
It took me about a week to get my head around pureMVC. Granted it may take a little more time up front to get my application setup. However, I can already tell that as soon as my basic application structure is setup the development will fly.

Thanks alot!
« Last Edit: August 03, 2008, 04:13:11 by puremvc » Logged
jinglesthula
Newbie
*
Posts: 8


View Profile Email
« Reply #5 on: August 01, 2008, 03:01:45 »

I find sometimes seeing several examples helps a concept become clearer.  I was stalled on a similar issue til I discovered the onRegister function.  I'll include an account here for reference.

Scenario:

In the constructor for myMediator I retrieve a proxy which handles a web service and then call a method named 'send' on the proxy.

Currently, since I don't have my web service set up yet I have hard coded some test data for the proxy to send as a notification body.  This notification is sent in the 'send' method.  When the web service is set up and working, the 'send' method on the proxy will add a listener to the web service and invoke it.  An 'onResult' handler in the proxy will then send the notification with the data as body.

Currently the code executes in this order:
  • view component fires creationComplete event
  • a mediator listening to this event registers a mediator for the new view component
  • in the new mediator's constructor the proxy is retrieved
  • the 'send' method is invoked
  • the 'send' method sends a 'dataReady' notification (with the test data)
  • the new mediator's constructor exits and it is registered with the view
  • as part of the registration with the view, the mediator is interrogated for it's notification interests, which happen to include the 'dataReady' notification (so that it can display said data)

You see my problem.  In the final version, the mediator will have finished being registered with the View and interrogated before the web service comes back, thus allowing the mediator to respond to the 'dataReady' notification.

Moving the call to the web service to the onReady function of my mediator means that the mediator has been interrogated and is ready to respond to the notification by the time the proxy returns data (be it hardcoded or from the actual service itself).  This seems to work just fine.

Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #6 on: August 03, 2008, 04:22:44 »

I cleaned up the code in this thread for the benefit of future generations. Good to hear everyone's on the right track with this.

Use of onRegister will be added to the next edit of the Best Practices guide, and sooner into a FAQ. Basically, don't do facade manipulation in your Mediator and Proxy constructors, defer that until onRegister. Don't start any conversations you're not ready to participate in.

And, yes, there are demos still in the repo (most of them, actually) that do this. But such is the evolving nature of best practices. onRegister didn't come around until 2.0.

-=Cliff>
Logged
Pages: [1]
Print