Flex Tutorials

Flex Singleton Class

Written by Philip

The Singleton class, which can be used in Flex / Flash, which are of course coded in AS3, is designed to restrict instantiation of a class to only one object. It's great when you only want one object across the whole of your application. An example could be you load in a load of xml data and you want to store the values of the xml for use throughout the application. In Flex / Flash we can write a Singleton Class like the following. We use the getInstance() to return the object created.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.howtocode
{
[Bindable]
public class Singleton
{
 
/** Only one instance of the model locator **/
 
private static var instance:Singleton = new Singleton();
 
/** Bindable Data **/
 
public var flexData:String = "SuperApp";
public var userScore:int = 0;
 
public function Singleton()
{
if(instance)
{
throw new Error ("We cannot create a new instance.
 Please use Singleton.getInstance()"
);

}
}
 
public static function getInstance():Singleton
{
return instance;
}
}
}


Singleton Class Access

To access a variable from the Singleton class we use the following.

 

1
2
3
4
5
6
7
8
9
import com.howtocode.Singleton;
 
private var model:Singleton = Singleton.getInstance();
 
// To retrieve flexData String from Singleton Class use model.flexData
trace(model.flexData);
 
// To set flexData String in Singleton Class use the following
model.flexData = "FlexApp"

 

You can of course do something really cool which is place something like place

 

1
<mx:Button id="flexButton" label="{model.flexData}">

 

into you're MXML and when flexData is changed in your Singleton Class it automatically updates you're MXML as we've set the whole Singleton Class as [bindable].

 

Deferred Instantiation

Written by Philip

Deferred Instantiation

Article on Flex Application Performance and how to improve it.

http://www.adobe.com/devnet/flex/articles/client_perf_06.html

 

Flash Memory Usage

Written by Philip

Website / Blog : ( Your Website / Blog URL ie. www.how-to-code.com )

Contact : ( Your Email ie. This e-mail address is being protected from spambots. You need JavaScript enabled to view it )

Detect Memory Usage in Flash and Flex SWFs

http://www.websector.de/blog/2007/10/01/detecting-memory-leaks-in-flash-or-flex-applications-using-wsmonitor/

 

   

Flex 3 Component Explorer

Website / Blog : www.how-to-code.com

Contact : This e-mail address is being protected from spambots. You need JavaScript enabled to view it

The Flex 3 Component Explorer is a good way to browse through Flex 3 Components. http://examples.adobe.com/flex3/componentexplorer/explorer.html

If you're looking at this you might be interested in looking at Tour de Flex as well! Which is another component based explorer apart from with 3rd party components that are amazing!

 

Flex 3 Style Explorer

Written by Philip

Website / Blog :  www.how-to-code.com

Contact : This e-mail address is being protected from spambots. You need JavaScript enabled to view it

The Flex 3 Style Explorer is a great way to check UI Components and see what they look like using CSS to style them. You're then able to copy and paste the CSS into your own work.

http://examples.adobe.com/flex3/consulting/styleexplorer/Flex3StyleExplorer.html#

 

   

Page 1 of 2