0
votes

Ok this probably sounds dumb but Im a complete beginner in Flex programming.

I have an application with a main .mxml file, and a certain class Foo that I call from the .mxml

In Foo, I make a URLRequest and listen for the Complete event. Then I found myself with the returned data in a Foo function, but I have no idea how to communicate it to the .mxml part of the applicaton ! I looked into ArrayCollections but I can't seem to understand how they work and whether it might help. Isn't there a way to modify, from inside the class, a variable with a global scope ?

5

5 Answers

2
votes

This sounds like a small application, but if it's a large application you might want to look at a micro-framework like RobotLegs

If you have your Foo class extend EventDispatcher then it will be able to send events and have the main MXML app listen for said events.

package com.example
{
    import flash.events.EventDispatcher;
    import com.example.events.MyEvent;

    public class Foo extends EventDispatcher 
    {
        public function doAction():void 
        {
            var someData:String = "blah";
            dispatchEvent(new MyEvent(MyEvent.SOMETHING_HAPPENED, someData));
        }
    }
}

A Custom event with a payload (in this case a string)

package com.example.events 
{
    import flash.events.Event;

    public class MyEvent extends Event 
    {
        static public const SOMETHING_HAPPENED:String = "somethingHappened";

        private var _myData:String;

        public function get myData():String 
        {
            return _myData;
        }

        public function MyEvent(type:String, myData:String, bubbles:Boolean=false, cancelable:Boolean=false) 
        {
            _myData = myData;
            super(type, bubbles, cancelable);
        }

        override public function clone():Event 
        {
            return new MyEvent(type, myData, bubbles, cancelable);
        }
    }
}

Working with your Foo class from the main file:

public function EventDispatcherExample() {
    var foo:Foo = new Foo();
    foo.addEventListener(MyEvent.SOMETHING_HAPPENED, actionHandler);
    foo.doAction();
}

private function actionHandler(e:MyEvent):void {
    trace("my data is: " + e.myData);
}
1
votes
import mx.core.FlexGlobals;
FlexGlobals.toplevelApplication.varName;
1
votes

Your Foo class can dispatch an event and have something in you main.mxml listen for that event. I am sure that I could create an example. I think it is under customer events in Flex documentation. This is assuming I understand the question.

1
votes

As John said, an event is your best choice.

If you'd like some example code, I provided some for a similar question here: Data from Popup to Main Application?

0
votes

An event might be the best way to do it as it has been stated. Another approach is to dispatch an event like this

dispatchEvent(new Event('somethingHappened'));

and also create a get method in your class for the data you need to get.

Then all you have to do in your main app is this

var foo:Foo = new Foo();
foo.addEventListener('somethingHappened', actionHandler);

private function actionHandler(e:Event):void 
{
    trace(foo.memberData);
}

This way might be more suitable if the data should be a class member anyway and if you would like to avoid creating a new event class.