Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variable in Flex

I am trying to work with a global variable in Flex and it does not appear to be working correctly.

In my default mxml file I declare the following

           public var appID:int;

This variable keeps track of the session ID across my application for SQL purposes. On another mxml page I call the following code which should update the global variable to the current ID.

                // Get the ID
            sqlStatement.text =
                "SELECT Max(id)FROM applications";
            sqlStatement.execute();

            var result:SQLResult;

            result = sqlStatement.getResult();
            FlexGlobals.topLevelApplication.appID = result.data[0];

Lastly I run a SQL Update query using the ID as a parameter. My problem is that the FlexGlobals.topLevelApplication.appID is always 0, for some reason the global variable never gets updated, I have checked to ensure that result.data[0] is correct but the value never gets passed to the global variable.

Does anyone see what I could be doing wrong here? or does anyone have a better suggestion for keeping track of the id across my application?

Thanks for any help in advance!

like image 808
korymiller Avatar asked Mar 03 '26 09:03

korymiller


2 Answers

I like to create a "Globals" class that has static variables:

package {
    public class Globals {
        public static var APP_ID:int;
    }
}

then access it in another class with :

Globals.APP_ID = result.data[0];
like image 114
Corey Avatar answered Mar 05 '26 09:03

Corey


When I started I found the mix of mxml and AS3 with Zend Framwork, php, and WAMP setup a little daunting, and struggled to come to terms with Flex as a programming environment. The use of global variables eased this pain a little.

The way which worked for me ... although I am sure there may be better examples out there is the following;

First create a Globals class

package
{
    public class Globals
    {
    [Bindable] public var name                      : String;
    [Bindable] public var email                     : String;
    [Bindable] public var lecturersResult           : CallResponder;
    [Bindable] public var studentsResult            : CallResponder;

    [Bindable] public var datagrid_lecturers        : DataGrid;
    [Bindable] public var dropDownList_lecturerid   : DropDownList;
    [Bindable] public var DatabaseTables            : Accordion;    
    [Bindable] public var lectAdd                   : Button;

    // auto generated classes by Zend Framework, where references are setup here

    [Bindable] public var lecturers                 : Lecturers;
    [Bindable] public var students                  : Students;

        public function Globals()
        {
        // variables initialised

            name                = "";
            email               = "";
            lecturersResult             = new CallResponder();
            studentsResult              = new CallResponder();
            lectAdd                     = new Button();
            dropDownList_lecturerid     = new DropDownList();
            datagrid_lecturers          = new DataGrid();
            DatabaseTables              = new Accordion();

            lecturers                   = new Lecturers();
            students                    = new Students();
        }

        // also useful when creating functions used between classes.

        public function func1()
        {
        }
        public function func2()
        {
        }
        public function func3()
        {
        }
    }
}

in the main project mxml file I declared an instance of the Globals class

public var g:Globals = new Globals();

mxml code fragment using the new instance

<s:DataGrid id="datagrid_lecturers" x="10" y="10" width="860" height="300" editable="true" fontSize="12" requestedRowCount="4"
                creationComplete="g.lecturersService.datagrid_lecturers_creationCompleteHandler(event,g,datagrid_lecturers,btnAddLecturer,btnDeleteLecturer,btnUpdateLecturer)" 
                selectionChange="g.lecturersService.datagrid_lecturers_selectionChangeHandler(event,g)">

Another example ...

g.lecturersResult.token = g.lecturersService.getAllLecturers();

The downside, I carry around alot of baggage, probably more binding than is necessary. For as long as the database is small this will work ok. Purists will not like this approach. Gloabls are often referred as bad programming practice ... which I agree, but for those starting off ... it is not a bad way to go. The good porogramming bits can some later as you become better/smarter at coding in this environment.

like image 43
SmileySnr Avatar answered Mar 05 '26 08:03

SmileySnr