--- In flexcoders@yahoogroups.com, "Wally Kolcz" <wko...@...> 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

Reply via email to