I think the piece that you were missing is that load is actually pulling
data in from a service, and you can't rely on the order in which the service
responses return.
Josh's answer above is definitely a best practice, if you have multiple
service calls that need to have responses handled in a certain order, wait
and "chain" them by not calling the second service until the first service
returns.
And to answer your original question about putting data in an event, simply
extend the event class and then you can define any properties you want, and
access those properties in your event handler, here's a quick example:
package events
{
import flash.events.Event;
public class LoadEvent extends Event
{
public static const DATA_LOAD:String = "dataLoadEvent";
public var message:String;
public function LoadEvent(message:String, bubbles:Boolean=false,
cancelable:Boolean=false)
{
this.message = message;
super(DATA_LOAD, bubbles, cancelable);
}
}
}
//add the event listener before calling your load method:
loader.addEventListener(LoadEvent.DATA_LOAD,loadResultHandler);
//raise the event in the response in your loader class
dispatchEvent(new LoadEvent("Data Loaded Successfully, true, true));
//pull the data out in your handler
private function loadResultHandler(loadEvent:LoadEvent):void
{
var message:String = loadEvent.message;
trace(message);
}
Hope something in there helps
On Mon, Jun 9, 2008 at 10:02 PM, Josh McDonald <[EMAIL PROTECTED]> wrote:
> If you need responses to come back in order, don't make the second
> request until you've got the first result. Or wait for all responses, and
> put them in an array based on some order-information in the AsyncTokens.
>
> -Josh
>
>
> On Tue, Jun 10, 2008 at 11:48 AM, robbarreca <[EMAIL PROTECTED]>
> wrote:
>
>> I'd love to know if my explanation made any sense when someone gets a
>> free moment. If it sounds like I'm smoking crack, please let me know
>> and I'll check myself into rehab.
>>
>> Thanks!
>>
>> -R
>>
>>
>> --- In [email protected] <flexcoders%40yahoogroups.com>,
>> "robbarreca" <[EMAIL PROTECTED]> wrote:
>> >
>> > So when I say "thread" maybe I'm using the wrong terminology. I'm not
>> > actually talking about two different users/browsers. I'm talking about
>> > the same browser running two calls of load().
>> >
>> > Let me expand:
>> >
>> > function onInitialize() {
>> > loader.addEventListener(handleComplete);
>> > load(1); // Say this takes 20 seconds to complete load
>> > load(2); // But this one only takes 5 seconds to complete
>> > }
>> >
>> > private var storedFlag:uint = 0;
>> >
>> > function load(flag:uint) {
>> > storedFlag = flag;
>> > loader.loadSomeStuff();
>> > }
>> >
>> > function handleComplete(event:Event) {
>> > // What is storedFlag here?
>> > }
>> >
>> > The execution goes like this:
>> >
>> > load(1);
>> >
>> > load(2);
>> >
>> > handleComplete(event1); // Triggered from the load(1) which is the
>> > race since this storedFlag == 2, but this is handling the completion
>> > of the first load. I really want to get that flag data from the event
>> > object so there is no race condition.
>> >
>> > handleComplete(event2);
>> >
>> > This is a simplified example, but I really do have a use case for
>> > this. Did I do a better job of explaining this time?
>> >
>> > -R
>> >
>> > --- In [email protected] <flexcoders%40yahoogroups.com>,
>> "Daniel Gold" <danielggold@> wrote:
>> > >
>> > > It makes sense when dealing with concurrent programming, but Flash
>> > is single
>> > > threaded and you won't have a case where multiple threads are task
>> > switching
>> > > and simultaneously executing functions. Every function call and event
>> > > handler in flex is basically "in-lined" and will execute to
>> completion.
>> > >
>> > > On Mon, Jun 9, 2008 at 5:11 PM, robbarreca <rob@>
>> > > wrote:
>> > >
>> > > > Sorry, I must have not explained well. The race condition I'm
>> > talking
>> > > > about exists here:
>> > > >
>> > > > thread 1: calls load() and say someFlag gets set to 1;
>> > > > thread 2: calls load() and someFlag is set to 2;
>> > > > thread 1: the first load is complete and handleComplete() is called,
>> > > > but someFlag is set to 2 when the value I want is 1.
>> > > >
>> > > > I want to attach a copy of the value of someFlag to the event so in
>> > > > handleComplete() I could call event.target.someFlagCopy and
>> always get
>> > > > the value that was set in *that* thread's load() call.
>> > > >
>> > > > Does that make any sense?
>> > > >
>> > > > -R
>> > > >
>> > > >
>> > > > --- In [email protected]
>> > > > <flexcoders%40yahoogroups.com><flexcoders%
>> 40yahoogroups.com>,
>> > "Daniel
>> > > > Gold" <danielggold@> wrote:
>> > > > >
>> > > > > does determineFlag() do some asynch service call? If so you
>> need to
>> > > > wait and
>> > > > > raise an event or update a var in a model and listen for changes
>> > > > that way.
>> > > > > Otherwise your determineFlag() method will run to completion
>> > before the
>> > > > > loader.loadSomeStuff() line is executed, Flash is single threaded
>> > > > for user
>> > > > > code execution so you shouldn't have a race condition there
>> > > > >
>> > > > > On Mon, Jun 9, 2008 at 3:55 PM, robbarreca <rob@>
>> > > > > wrote:
>> > > > >
>> > > > > > Say I have two functions load() and handleComplete(event:Event).
>> > > > Right
>> > > > > > now to get custom data to handleComplete I do something like
>> this
>> > > > > >
>> > > > > > private var someFlag:uint = 0;
>> > > > > >
>> > > > > > function load() {
>> > > > > > loader.addEventListener(handleComplete);
>> > > > > > someFlag = determineFlag();
>> > > > > > loader.loadSomeStuff();
>> > > > > > }
>> > > > > >
>> > > > > > function handleComplete(event:Event) {
>> > > > > > trace(someFlag);
>> > > > > > }
>> > > > > >
>> > > > > > But if I call this super fast, someFlag is gonna be wrong.
>> > I've seen a
>> > > > > > method where you can add an anonymous function somehow, but
>> > I'm pretty
>> > > > > > sure that even faced the same race condition problem. What
>> is the
>> > > > > > *proper* way to go about this?
>> > > > > >
>> > > > > >
>> > > > > >
>> > > > >
>> > > >
>> > > >
>> > > >
>> > >
>> >
>>
>>
>
>
> --
> "Therefore, send not to know For whom the bell tolls. It tolls for thee."
>
> :: Josh 'G-Funk' McDonald
> :: 0437 221 380 :: [EMAIL PROTECTED]
>
>