views:

128

answers:

4

I have a main application which has an int variable declared. I want to access this variable in another component which is present in another package. How can I do this?

Main.mxml (default package)

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                               preinitialize = "foo()">
    <fx:Script>
            <![CDATA[
                         public var value1:int;
                         public function foo():void {
                            value1 = 5;
                         }

                ]]>
        </fx:Script>

    <\s:Application>

Comp.mxml (components package)

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
                 creationcomplete = "foo2()">
           <fx:Script>
                <![CDATA[
                            public function foo2(): void{
                             //------> access value1 from Main.mxml here.
                            }

                    ]]>
            </fx:Script>

</s:Group>
A: 

You need to have some kind of redirection, like telling the component where to look, i.e.:

<fx:Script>
  <![CDATA[
    [Bindable] public var myapp:Application = null;
    public function get value1():int 
    { 
      return ( myapp == null ? 0 : myapp.value1 ); 
    }
    public function foo2():void 
    {
      var i:int = this.value1;
    }
  ]]>
</fx:Script>

And in application:

<package:Comp myapp="this" />

Otherwise, you could create a singleton of some sort - a class-wide static variable. But that's pretty messy.

eruciform
+1  A: 

Since the variable you need is in the main application class, you can use the parentApplication property to access it.

Main(this.parentApplication).value;
//or
Main(Application.application).value;

In general, the correct way of doing this is to pass a reference of the source object (in this case, the application) to the caller object - or pass the value itself through some public property. Assuming that Comp in question is present in the main app itself, you can do:

<!-- Main.mxml : make value1 [Bindable] if you need the updates to be 
    reflected automatically  -->

<custom:Comp val="{this.value1}"/>

<!-- Comp.mxml : declare val as a public variable/property -->
<fx:Script>
    <![CDATA[
      public var val:int;
    ]]>
</fx:Script>
Amarghosh
A: 

Hi

Your main app should have a reference to the component what needs the int value. And the value should be set from you main app. If the value needs to be set in response to an event in the component, the the component could trigger and dispatch an event, and the main app could have an event listener, and then pass the required int.

You could also bind the values in the main apps actionscript: BindingUtils.bindProperty(this, "i", this.component, "i"), but the previous method is a better practice.

Brian Bishop
A: 

How about Custom Events.

http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html

When you create your custom event, you can access the variable across the application.

Otherwise go for

mx.core.Application.application to access the main application

Vinothbabu