Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:36:29+00:00 2026-05-15T02:36:29+00:00

I’ve created an AS class to use as a data model, shown here: package

  • 0

I’ve created an AS class to use as a data model, shown here:

package
{   
    import mx.controls.Alert;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    public class Model
    {

        private var xmlService:HTTPService;
        private var _xml:XML;
        private var xmlChanged:Boolean = false;

        public function Model()
        {
        }

        public function loadXML(url:String):void
        {
            xmlService = new HTTPService();
            if (!url)
                xmlService.url = "DATAPOINTS.xml";
            else
                xmlService.url = url;

            xmlService.resultFormat = "e4x";        
            xmlService.addEventListener(ResultEvent.RESULT, setXML);
            xmlService.addEventListener(FaultEvent.FAULT, faultXML);
            xmlService.send();              
        }

        private function setXML(event:ResultEvent):void
        {
            xmlChanged = true;
            this._xml = event.result as XML;
        }

        private function faultXML(event:FaultEvent):void
        {
            Alert.show("RAF data could not be loaded.");
        }

        public function get xml():XML
        {
            return _xml;
        }           

    }
}

And in my main application, I’m initiating the app and calling the loadXML function to get the XML:

<fx:Script>
    <![CDATA[
        import mx.containers.Form;
        import mx.containers.FormItem;
        import mx.containers.VBox;
        import mx.controls.Alert;
        import mx.controls.Button;
        import mx.controls.Label;
        import mx.controls.Text;
        import mx.controls.TextInput;

        import spark.components.NavigatorContent;

        private function init():void
        {   
            var model:Model = new Model();
            model.loadXML(null);
            //the following line executes before model.loadXML has finished...
            var xml:XML = model.xml;
        }   

    ]]>
</fx:Script>

The trouble I’m having is that the getter function is running before loadXML has finished, so the XML varible in my main app comes up undefined in stack traces. Specifically, the loadXML function called ResultEvent.RESULT, then jumping to setXML, etc…the code in the main app continues to execute while loadXML waits for a result, so the getter in the main app (var xml:XML = model.xml;) executes before the variable has been defined by setXML.

How do I put a condition in here somewhere that tells the getter to wait until the loadXML() function has finished before running?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:36:30+00:00Added an answer on May 15, 2026 at 2:36 am

    Your model should extend EventDispatcher and (with custom events) dispatch an event indicating that the model has been successfully loaded. This is needed because Flex is Asynchronous…

    Then in the Application after create the model just add the event listener to the load event, and in there is where you can start using the model vars

    Don’t forget to dispatch and listen to any error events!

    HTH
    Gus

    Something like this…

    in the main app:

    var model:Model = new Model();
    // registering the listener
    model.addEventListener(ModelEvent.LOADED, model_loadedHandler);
    model.loadXML(null);
    

    then the listener

    private function model_loadedHandler (event:ModelEvent):void
    {
        var xml:XML = model.xml;
    }
    

    and finally, in the model:

    private function setXML(event:ResultEvent):void
    {
        xmlChanged = true;
        this._xml = event.result as XML;
        var modelEvent:ModelEvent = new ModelEvent(ModelEvent.LOADED); 
        // you can add other info to the event like the XML
        dispatch(modelEvent);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 459k
  • Answers 459k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Rather than having the Legs a field of the abstract… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer Some kind of heartbeat or ping signal, sent from client… May 15, 2026 at 11:21 pm
  • Editorial Team
    Editorial Team added an answer I am exporting the JAVA_HOME and the path environment variable… May 15, 2026 at 11:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.