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: Loading multiple information in a command  (Read 9540 times)
dierre
Jr. Member
**
Posts: 11


View Profile Email
« on: January 03, 2009, 03:49:56 »

Hi, I'm having this problem: I have a loginCommand. Inside this command there is...let's say UserProxy.login which is the real login but then I have to load more information (in my case I have to load the item possesed by the logged user).
So I basically did this.

:
var _userlogin:User = notification.getBody() as User;
var _cuproxy: UsersItemsProxy = facade.retrieveProxy( UsersItemsProxy.NAME ) as UsersItemsProxy;
var _userproxy:UserProxy = facade.retrieveProxy( UserProxy.NAME ) as UserProxy;
_userproxy.login( _userlogin.username, _userlogin.password );
Alert.show(_userproxy.users[0].id);
// here i should load the items possessed by the user

The problem is that the user is loaded AFTER the Alert.show so I have an error of bad index at running time.

I'm using the delegate because I use the service in more proxy.

edit: I saw the AsyncCommand but I don't think it's the right thing to do because the command is just one: login, but I have to load more information from different proxies.
« Last Edit: January 03, 2009, 03:55:39 by dierre » Logged
dierre
Jr. Member
**
Posts: 11


View Profile Email
« Reply #1 on: January 03, 2009, 04:06:56 »

I was just reading the Best Pratices, you basically do the same thing with the GetPrefsCommand, so I need to use two distinct command, don't I?
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #2 on: January 04, 2009, 07:21:05 »

Use the AsyncCommand utility. Make an AsynchMacroCommand and have the first subcommand be an AsyncCommand that doesn't call its completion callback until the proxy has the data.

How to do this? Call the proxy and pass it not only the data it needs to try the login, but also a reference to the AsyncCommand's onComplete callback method.

Have the proxy get the add this to the async token that is returned from the service call. Then when the call returns,the onComplete method reference will be on the token property of the ResultEvent, so call it. This will cause the AsyncMacroCommand to execute the next subcommand.

-=Cliff>
Logged
dierre
Jr. Member
**
Posts: 11


View Profile Email
« Reply #3 on: January 05, 2009, 06:31:59 »

thanks, just a question. I use a delegate, so my login method is:

:
public function login( username:String, password:String ):void
{
var delegate:UserService = new UserService( new Responder ( loginResult, onFault ) );
delegate.login( username, password );
}

from here where should I do the trick with the token? loginResult?

ps: I'm sorry, I'm really new to as3.
Logged
puremvc
Global Moderator
Hero Member
*****
Posts: 2871



View Profile WWW Email
« Reply #4 on: January 05, 2009, 08:31:56 »

This is made more complicated with the use of a delegate, which is only needed if multiple Proxies need to call the same service.

If only one Proxy accesses the service, you can do the call inside the Proxy itself, since the delegate will never be used elsewhere. This greatly simplifies things.

Regardless of using a delegate or just making the service call from the Proxy, whenever you call HttpService's send method you get back an AsyncToken object. Many times this is simply discarded by the caller who never even assigns the return value to a variable. But you need it.

The AsyncToken is used to keep track of any info you need to handle an asynchronous call's return when it comes back to you. It is a dynamic object that you can add arbitrary properties to. In this case, we want the AsyncCommand to pass a reference to its onComplete method to the Proxy which will either pass it on to the delegate along with the login info, or simply make the call and add the reference to the token itself.

If using a delegate, when the call is made, be sure to set the onComplete (Function type) reference on the token. The delegate sets up the Proxy's result handler as in the responder, and when it is called, it should get the data from the ResultEvent and stor it as its data property and then call the onComplete method on the token object that is a property of the ResultEvent.

-=Cliff>
Logged
dierre
Jr. Member
**
Posts: 11


View Profile Email
« Reply #5 on: January 05, 2009, 09:13:56 »

Yes, I'm using delegates because I need to use that service in two proxies.
BTW I'll try, thanks.
Logged
dierre
Jr. Member
**
Posts: 11


View Profile Email
« Reply #6 on: January 05, 2009, 09:29:27 »

Just a question about "Best Pratices", in the LoginProxy didn't you forget to call the service.login inside the proxy login method?
Logged
Pages: [1]
Print