function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bryanobryano 

Flex Toolkit, having a callback function return a value

I'm using the Apex Flex Toolkit.  I call a function and within that function I make a query.  I have the query's callback function return a string.  Is there a way for the original function to return that string?

For example, I'm trying to do something like this:

Code:
var soql:String = "Select Id from document where Name = 'imageName'";
var pathToImage:String = getPath(soql);

private function getPath(soql:String):String {
    apex.query(soql, new AsyncResponder(function (qr:QueryResult):String {
        // Work with QueryResult and return a string
        return <some string>;    
    }));
    
    return <same string from callback>
}

 

Ron HessRon Hess
no, this won't work.
basicaly the outer function has already returned before the inner ( async) ever gets called.


bryanobryano
Well, since the apex.query() returns void and asynch is the only way to call it, is there a way to achieve what I want to do?
Ron HessRon Hess
i'm not sure exactly what you are trying to do, if you are trying to turn an async processing model ( Flex HTTP request) into a sync model, i would have to say why? it makes your app appear hung as no other processing occurs if you are in a busy wait loop while the async request process proceeds.

I understand that Async processing is a new mind-set, however, I feel it is well worth learning.
Basicaly (in flex apps) the async process happens , then your callback calls the next function that needs this data, eventualy updating the UI by binding the results to a UI element. 

you can build a busy wait loop, but i don't think your app will perform the way you want if you do this.

This (sync) alternative is (write your callback to ) fire an event, which has the result, then in the outer function you make the query, and enter a busy wait loop (forever loop) until you recieve the event sent by the callback.  I don't have any code for this, and i would not recomend building an app that does this.  This method defeats the async processing.
bryanobryano
Thanks Ron for all your help.  I do realize that asyhnc calls are the way to go.  However, let me try to explain what I'm trying to accomplish.  I have an app that contains images that are stored as documents.  When I initiliaze the app, I need to load the images.  To do this, I do a query on the document to get the id of the record and use "/servlet/servlet.FileDownload?file=<image Id>" for the source of the image.  You might ask, why I don't just hard code that URL in the first place.  I can't do that since my app will be placed on different salesforce orgs and the ids of the images will be different.

Since the app will be on different orgs, I need to do the following when the app is loading:
1. Query document object to get the id
2. Set the source path of the images with the correct URL
3. Finish loading the app

Maybe I'm going about this wrong.  Perhaps there is a better way to get images to display when I attach the .swf file to the s-control.




bryanobryano
I think I'll just embed the images instead of trying to load them at run time.
Ron HessRon Hess
bingo, simple is best.
devguydevguy
You could also set the image source directly in your AsyncResponder.  And if the image is not available in your class or .as file, you'll have to pass in the image in to the getPath() method and set it to a class- or .as-scoped variable.