Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Locked
0

Global variables in flex

New Here ,
Oct 01, 2010 Oct 01, 2010

Hi all,

How do we create and use global variables in Flex 4.0.

I tried the following

Created the following on mxml file.

public var gbString:String;

and used the following line in another mxml file.

txtTest.text = FlexGlobals.topLevelApplication.gbString;

While running this file, it shows the following error.

ReferenceError: Error #1069: Property gbString not found on dbtest1 and there is no default value.

at dbtest1/button1_clickHandler()

at dbtest1/___dbtest1_Button1_click()

Please help me

Thanks in advance

Ajay

10.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 01, 2010 Oct 01, 2010

Hi Ajay,

Have you declared the below variable in your main mxml file or any other file..??

public var gbString:String;

In order to access the gbString variable using  FlexGlobals.topLevelApplication.gbString you need to declare it main mxml file.

Thanks,

Bhasker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 01, 2010 Oct 01, 2010

Dear Bhasker

Thanks for your replay.

I declared the variable in the main file.

See the code

dbtest.mxml

public var gbString:String;

protected function button1_clickHandler(event:MouseEvent):void

{

     FlexGlobals.topLevelApplication.gbString="this is sample";

     navigateToURL(new URLRequest("dbtest1.html"),"_self");

}

dbtest1.mxml

protected function button1_clickHandler(event:MouseEvent):void

{

     txtTest.text = FlexGlobals.topLevelApplication.gbString;

     // Error shows in this line.

}

Regards

Ajay

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 01, 2010 Oct 01, 2010

Hi Ajay,

If you have decalred it in the main mxml file then it should work for you correctly.

Can you post your total code in the dbtest.mxml ..???

Thanks,

Bhasker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 01, 2010 Oct 01, 2010

Dear Bhasker

Below is the code. 

dbtest.mxml

------------------

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" name="MyApp"

   xmlns:s="library://ns.adobe.com/flex/spark"

   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

<fx:Script>

<![CDATA[

import flash.net.navigateToURL;

import flashx.textLayout.utils.NavigationUtil;

import mx.accessibility.AlertAccImpl;

import mx.controls.Alert;

import mx.core.Application;

import mx.core.FlexGlobals;

import mx.events.FlexEvent;

public var gbString:String;

protected function button1_clickHandler(event:MouseEvent):void

{

// TODO Auto-generated method stub

FlexGlobals.topLevelApplication.gbString="this is sample";

//Alert.show(FlexGlobals.topLevelApplication.gbString);

navigateToURL(new URLRequest("dbtest1.html"),"_self");

}

]]>

</fx:Script>

<fx:Declarations>

<!-- Place non-visual elements (e.g., services, value objects) here -->

</fx:Declarations>

<s:Button x="275" y="256" label="Button" click="button1_clickHandler(event)"/>

</s:Application>

dbtest1.mxml

--------------------

<?xml version="1.0" encoding="utf-8"?>

<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" minWidth="955" minHeight="600">

<fx:Script>

<![CDATA[

import com.adobe.fiber.core.model_public;

import flash.utils.flash_proxy;

import mx.controls.Alert;

import mx.core.Application;

import mx.core.FlexGlobals;

import mx.events.FlexEvent;

protected function button1_clickHandler(event:MouseEvent):void

{

// TODO Auto-generated method stub

txtTest.text = FlexGlobals.topLevelApplication.gbString;

//Alert.show(FlexGlobals.topLevelApplication.gbString);

}

]]>

</fx:Script>

<fx:Declarations>

<!-- Place non-visual elements (e.g., services, value objects) here -->

</fx:Declarations>

<s:Button x="275" y="256" label="Button" click="button1_clickHandler(event)"/>

<s:TextInput x="217" y="117" id="txtTest"/>

</s:Application>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 01, 2010 Oct 01, 2010

The second mxml file, from wich you trying to retreive your global property, might be not an Application. It can be a mxml extending Group, TitleWindow, or other. Because, when you develop a Project, you can only have 1 main app. But, in the first application everything writed correctly, and you can access all public propertis of it by calling to a FlexGlobals.topLevelApplication.propertyName. But, for once again, not from another Application.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 01, 2010 Oct 01, 2010

HI Banditizm

I am a beginner to flex. But having good experience in C, VB etc.

My requirement is to share values in different mxml files. Dont know much about application, mxml extending group etc. 

Can u suggest a method to improve.

With thanks

Ajay

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 01, 2010 Oct 01, 2010

Create a singleton class and use it through your whole application.

Wherever your need a value you do something like this:

var neededProperty:PropertyType = SingletonClass.getInstance().neededProperty;

public class SingletonClass {

public var neededProperty:PropertyType = new PropertyType();

public static function getInstance():SingletonClass

.....

etc

}

Somewheve in Main you would set this property

SingletonClass.getInstance().neededProperty = value;

You'll find several examples of Singletons across the web.

Now the cleanest and most testable way but it will do the work. Not sure what

you are trying to achieve though...

HTH,

C

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 02, 2010 Oct 02, 2010

Can't say anything about SingletonClass, my bad. But I usually expire the FlexGlobals.topLevelApplication.anyPublicVarOfMainApplication. But, in your example, you use Application as a start tag of both mxmls, wich is wrong i suggest because you can only have one Application in your project. Try change your second mxml's start tag to a group, f.e. (the second i mean from wich you retrieve the Application's global value). Am i right? Or, you can use TitleWindow, or Panel, but not Application.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 02, 2010 Oct 02, 2010

Not sure exactly whats going on (im kind of a noob).  But you could declare a bindable public variable in the dbtest1.mxml script block.

After you do that, you can reference from the Main.mxml file.  Being able to access this variable will allow you to set it to your variable in the Main file.

PS:  If you do not know bindable syntax it is below.  In a custom component, it allows you to reference the variable from the parent object

[Bindable]

var i:int;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 02, 2010 Oct 02, 2010

Even I am new in Flex but just want to know ,

is it not possible to extend or inherit the main mxml and then use the global variable as required.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 03, 2010 Oct 03, 2010
LATEST

OK, such construction workable

<Application>

......

public var myProperty:String;

.....

</Application>

Then somewhere in project you use the

<mycomponents:Tester id="tester" />

or the

import mycomponents.Tester;

.....

var tester = new Tester();

mxml class named Tester:

<Group>

     .....

     public function test():void {

          Alert.show(FlexGlobals.topLevelApplication.myProperty)

     }

     .....

</Group>

and Then you call to it's function

tester.test();

this will show you an alert of Application's myProperty

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines