actionscript-3apache-flexactionscriptflex4.6flash-builder4.5

How to access parent or wrapper variables from an AS3 Worker Class


I am using Flash Builder 4.7 and have created a Worker Class. Below is the code:

package co.fuix.mobile.system.model
{
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.system.MessageChannel;
    import flash.system.Worker;
    import flash.utils.getDefinitionByName;

    import mx.managers.SystemManager;

    public class InstantMessengerWorker extends Sprite
    {

        private var bm:MessageChannel;
        private var mb:MessageChannel;
        public function InstantMessengerWorker()
        {

            super();

            bm = Worker.current.getSharedProperty("BACK_TO_MAIN_CHANNEL");
            mb = Worker.current.getSharedProperty("MAIN_TO_BACK_CHANNEL");
            mb.addEventListener(Event.CHANNEL_MESSAGE, onMainToBack);

        }

        protected function onMainToBack(event:Event):void
        {
            if(mb.messageAvailable){
                var s:SystemManager;
                trace('*'+mb.receive());
                trace('**'+mb.receive());
                trace('***'+mb.receive());
                trace(mx.core.FlexGlobals.topLevelApplication.myVariable);
            }
        }
    }
}

How do I reference a variable in the main mxml file. I know how to use message channels but I want to get that variable straight.

When I run the above code, this part

trace(mx.core.FlexGlobals.topLevelApplication.myVariable);

is giving me an error.

Any help will be regreatly appreciated


Solution

  • You can't access a variable from the main app like that. They are running separately. What you need to do is:

    Link to Adobe docs