I am building HTML5 applications at Sencha. It is a great platform, take a look...

 

  Ted Patrick - Developer Relations @ Sencha


   Note: This is the personal blog of Ted Patrick. The opinions and statements voiced here are my own.



Singleton in MXML

DIGG IT!     Published Wednesday, January 03, 2007 at 10:19 PM .

I am in Edmonton Canada this week working with Grant Skinner and his amazing team teaching them Flex. Today we talked a lot about MXML and AS3 Classes and when to use each. The thing is that they are 100% the same. MXML is translated into an AS3 class during the compilation process. There is really no different between MXML and AS3 Classes and they can be used interchangeably. No really! To show this I wrote a singleton in MXML. I have found this very handy for Settings Dialogs and Pop-ups where you want 1 and only 1 instance.

So here it goes:

<SettingsDialog xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>

private static var singleton:SettingDialog;

public static function getInstance():SettingsDialog
{
if ( SettingsDialog.singleton )
{
return SettingsDialog.singleton;
}else{
SettingsDialog.singleton = new SettingsDialog();
return SettingsDialog.singleton;
}
}

</mx:Script>

<mx:TextInput id="name" width="100">
<mx:TextInput id="age" width="100">
<mx:TextInput id="rank" width="100">
<mx:TextInput id="serial" width="100">
<mx:ColorPicker id="uiColor" >

<SettingsDialog>


Usage:
//call popupmanager with getInstance
mx.managers.PopUpManager.addPopUp( SettingsDialog.getInstance() , basePanel , true );



An AS3 Class and an MXML file are identical. Anything you can do in a Class you can do in MXML and vice versa. Plus this way your Singletons can have a UI too. Plus this way your SettingsDialog will maintain state naturally and you will never need to persist a thing. Anytime you pull up the settings dialog, it will be in the state you left it in last time, plus you can just get an instance and change the data in it without being on the DisplayList.

Cheers,

Ted :)


Where to find me:

Ted on Twitter - @__ted__
Ted on Adobe Groups
Ted on LinkedIn
Ted on Facebook
Ted at Adobe

Latest

Lists

Links

Jobs

city, state, zip

Archives