[flexcoders] Re: RemoteObject question/mystery

2009-11-02 Thread droponrcll


--- In flexcoders@yahoogroups.com, "Wally Kolcz"  wrote:
>
> Looking for a little education from all the people smarter than me of 
> remoteObjects. I have always used the MXML version which includes the 
> resultHandler attribute which made managing the returned data easy. However I 
> never learned the AS version very well. I can create the remoteObject in AS 
> and call its method, add the EventListener, but how do I actually get the 
> data back to a caller function?
> 
> index.mxml
> import org.mywindow.data.Users;
> 
> [Bindable] public var users:ArrayCollection;
> 
> public function getMyData():void {
>  users = Users.getAll();
> }
> 
> for the User's getAll function I have something like:
> 
> UserDAO.as
> ...
> public var _users:ArrayCollection;
> 
> public function getAll():void {
> var UserDAO:RemoteObject = new RemoteObject();
> UserDAO.destination = "ColdFusion";
> UserDAO.source = "org.mywindow.data.userDAO";
> UserDAO.getAll.addEventListener(ResultEvent.RESULT, 
> resultHandler);
> UserDAO.getAll.addEventListener(FaultEvent.FAULT, faultHandler);
> }
> 
> public function resultHandler(e:ResultEvent):ArrayCollection{
>   _users = e as ArrayCollection;
> return users;
> }
> 
> I know some of the code may be off, but I am trying to understand how a non 
> called function (resultHandler) can return data to the called function in a 
> different class. So my question is, if the index.mxml calls the getAll 
> function in the UserDAO class, will the getMyData function still receive the 
> ArrayCollection from the resultHandler? If I were to fire this off, till the 
> result be that my users ArrayCollection in the index.mxml be populated in the 
> end? Thanks!

You should never ignore the kind of compiler warning that you ought to be 
seeing here (implicit coercsion of ResultEvent to possibly unrelated type 
ArrayCollection).  The first thing you should do is put a break point inside 
resultHandler and look at the e object in the variables window, to see what 
property of it might contain the ArrayCollection you're looking for.  Then cast 
e[thatProperty] to ArrayCollection explicitly.

The next thing you need to realize is that the code that is calling your event 
handler is deep in EventDispatcher, and it does not give a flying, er, hoot, 
what your function returns.  In fact, I'd be really suprised if you are not 
getting a compiler or at least a run time error with your code as it is.

Event handler functions should _always_ have a return type of void, which means 
that in your result handler you either need to work with the AsyncToken on the 
result (which you'd set up when you call the RemoteObject) or you need set a 
variable or call a function which both your result handler and whatever needs 
to use it have access to (in their scope).

For more on using AsyncToken, see 
http://flexdiary.blogspot.com/2008/11/more-thoughts-on-remoting.html .

HTH;

Amy



[flexcoders] Re: RemoteObject question/mystery

2009-11-02 Thread jer_ela
how do I actually get the data back to a caller function?

you can't.  The request is async.  The caller has long since finished executing 
when the result comes back.

Have the resultHandler do whatever needs to be done with the result.

--- In flexcoders@yahoogroups.com, "Wally Kolcz"  wrote:
>
> Looking for a little education from all the people smarter than me of 
> remoteObjects. I have always used the MXML version which includes the 
> resultHandler attribute which made managing the returned data easy. However I 
> never learned the AS version very well. I can create the remoteObject in AS 
> and call its method, add the EventListener, but how do I actually get the 
> data back to a caller function?
> 
> index.mxml
> import org.mywindow.data.Users;
> 
> [Bindable] public var users:ArrayCollection;
> 
> public function getMyData():void {
>  users = Users.getAll();
> }
> 
> for the User's getAll function I have something like:
> 
> UserDAO.as
> ...
> public var _users:ArrayCollection;
> 
> public function getAll():void {
> var UserDAO:RemoteObject = new RemoteObject();
> UserDAO.destination = "ColdFusion";
> UserDAO.source = "org.mywindow.data.userDAO";
> UserDAO.getAll.addEventListener(ResultEvent.RESULT, 
> resultHandler);
> UserDAO.getAll.addEventListener(FaultEvent.FAULT, faultHandler);
> }
> 
> public function resultHandler(e:ResultEvent):ArrayCollection{
>   _users = e as ArrayCollection;
> return users;
> }
> 
> I know some of the code may be off, but I am trying to understand how a non 
> called function (resultHandler) can return data to the called function in a 
> different class. So my question is, if the index.mxml calls the getAll 
> function in the UserDAO class, will the getMyData function still receive the 
> ArrayCollection from the resultHandler? If I were to fire this off, till the 
> result be that my users ArrayCollection in the index.mxml be populated in the 
> end? Thanks!
>




[flexcoders] Re: RemoteObject question/mystery

2009-11-02 Thread creativepragmatic
> I know some of the code may be off, but I am trying to understand how a non 
> called function (resultHandler) can return data to the called function in a 
> different class. 

The way you are trying to do it, I don't think it can.


> So my question is, if the index.mxml calls the getAll function in the UserDAO 
> class, will the getMyData function still receive the ArrayCollection from the 
> resultHandler?

In the code you provided, I don't see how it could because getAll() does not 
return an array.


> If I were to fire this off, till the result be that my users ArrayCollection 
> in the index.mxml be populated in the end? Thanks!

_users in resultHandler() will be populated but it will not be returned in 
Users.getAll().

If you want to use an ArrayCollection across multiple classes in Flex, you 
might consider making it a Singleton.  If you need to manipulate the 
ArrayCollection, do it in the resultHandler().

I hope that helps.  I will monitor this thread if you have any other questions.