Loading an XML in ActionScript 3 before an event listener happens

I'm trying to fill some data from an xml feed we made into my Flash movie. The main action of this is in the constructor for MediaElementJS's .as file.

Anyway, the main problem is that I keep reading there is no way to load a URL synchronously in AS3 (which i still find hard to believe). This constructor calls both parse('url') and a function addEventListener(EVENT.ADDED_TO_STAGE, initializeAds);

Now the ads need info from the XML but the XML aint ready yet. I tried to call the ads at the end of the XML parser when I knew it would be prepped but it messes them up and the ad values never change from their defaults..

Oh wherefor art thou actionscript locking mechanism..

So is there anyway to preload data from a URL?

CODE:

    public function LoadXML(e:Event):void 
    {
        removeEventListener(Event.COMPLETE, LoadXML);
        var xmlData:XML = new XML(e.target.data);

        episodeData.pre.type    = xmlData.episode.pre.ad.@type;
        episodeData.pre.url     = xmlData.episode.pre.ad.text();

        episodeData.video       = Number(xmlData.episode.video.@id);    /*************** I CAN'T REMEMBER ***********/
        episodeData.pageTitle   = xmlData.episode.video.@pagetitle;
        episodeData.title       = xmlData.episode.video.@title;
        episodeData.source      = xmlData.episode.video.source.text();


        episodeData.post.type=xmlData.episode.post.ad.@type;
        episodeData.post.url=xmlData.episode.post.ad.text();

        episodeData.nextEpisode=xmlData.episode.post.nextepisode.text();    //if not empty redirect to this

        xmlLoading = false;
//THIS IS WHERE I TRIED TO CALL THE FUNCTION I NEED TO LOAD LATER
    }

    public function parse()
    {

        var xmlLoader:URLLoader = new URLLoader(); 
        //var xmlData:XML = new XML(); 
        xmlLoader.load(new URLRequest(rootURL + '/episode.aspx?associd=' + _episode)); 
        //xmlLoader.addEventListener(Event.COMPLETE, processXML);
        xmlLoader.addEventListener(Event.COMPLETE, LoadXML); 
    }

I've tried it with a static URL address and whatnot of course but no dice.

The code in the constructor works if I dynamically assign a static value but if I try chaining to events together to get the dynamic value and dynamic assignment it crunches.

In the constructor, definitely runs both by themselves:

        parse();

        // Google IMA EventListener
        addEventListener(Event.ADDED_TO_STAGE, initialize);

Upvotes: 0

Views: 776

Answers (2)

shaunhusain
shaunhusain

Reputation: 19748

Edit START

When I have multiple asynchronous calls that happen and I need something to happen after both of them are done I usually use booleans to store if each one has happened yet, then in a third function they both call I check both the booleans.

Here's how I'd do that:

protected function viewnavigatorapplication1_preinitializeHandler(event:FlexEvent):void
{
    var loader1:Loader = new Loader();
    var loader2:Loader = new Loader();

    loader1.addEventListener(Event.COMPLETE, loader1_completeHandler);
    loader1.load(new URLRequest("http://www.whitehouse.gov"));
    loader2.addEventListener(Event.COMPLETE, loader2_completeHandler);
    loader2.load(new URLRequest("http://www.nasa.gov"));
}

private function loader1_completeHandler():void
{
    loader1Done = true;

    //Maybe do some stuff here

    moveOn();
}
private function loader2_completeHandler():void
{
    loader2Done=true;

    //Maybe do some stuff here

    moveOn();
}


private function moveOn():void
{
    if(!loader1Done||!loader2Done)
        return;

    //Do whatever needs to be done once both asynchronous events have completed
}

If this isn't your problem I think you need to provide more of the code in place of the comments that indicate other things happen, because it is a bit unclear.

For example I'm not sure what you mean by "The code in the constructor works if I dynamically assign a static value but if I try chaining to events together to get the dynamic value and dynamic assignment it crunches." Also since there's no example of the data or what the rootURL is there's no way to debug from here to understand what's going wrong.

Since there's no error we would need to be able to re-compile some portion of your code locally to give any better feedback.

Edit END

Blocking or synchronous calls are a horrible idea with regard to network communications due to the lack of reliability of networks and/or servers. If a front end application locked up to wait for a response before doing any other processing it would result in a horrible user experience, this is why there is no synchronous remote calls.

What happens with a synchronous call when the server bombs out, the client remains locked even though no response will result, the user can't interact with anything else in the front-end because it's waiting for said response which will never come? It's much better that remote calls of any sort are done in an asynchronous fashion, the same is true with local disk access in Java (or otherwise) where using asynchronous non-blocking calls is generally a better way to go to allow the other processes within an application to continue regardless of the state or use on the disk.

What you're doing should work just fine, you make a call to a remote service, it responds with some result and hits your "listener" or "callback" function then you want to do something with the results you can call another function, and the data is there.

It sounds to me like the only thing that's not happening is updates after the fact aren't being reflected in the UI, this is probably due to a lack of Bindable metadata/event dispatching for the properties. Have you inspected the result in the event that returns, have you put breakpoints in the method that is meant to be called after the data has returned? What you're doing is completely possible and it even looks like you have most of it right, but there's definitely something your doing wrong that's resulting in you not being able to make this work. If you can explain the behavior a bit clearer that will help, also what do you do here:

//THIS IS WHERE I TRIED TO CALL THE FUNCTION I NEED TO LOAD LATER

Upvotes: 2

jhocking
jhocking

Reputation: 5577

Loading URLs is always asynchronous, so add the event listener in the response function for the URL loader.

Now your question sounds like you tried that but had some problem, so post that code and let us take a look.

Upvotes: 3

Related Questions