// you’re reading...

ActionScript 3.0

MXML Singletons in Flex 3

If you’re not clear on what a Singleton is, try glancing through my brief yet poignant article here: The Art and Beauty of Singletons.

Creating a Singleton through MXML

Okay, so I’ve been putting together a little image-viewer component in Flex, and I wanted it to load one data model that all other components would share. Sounds like a job for a Singleton, right? However, I also wanted to instantiate said model from MXML, which adds some kinks. In fact, I’ve heard it said that it’s not possible at all. And after some trial and error, I found that apparently it is possible- you just have to hack around a little bit.

If you instantiate your class (or “component”) through MXML, Flex will automatically call its constructor. Typically, this is a bad thing because you don’t call constructors directly in Singletons- instead of calling a method that returns a new object (i.e. a constructor), you call a method that returns a reference to an already-created object (getInstance() or instance()). Flex won’t do that for you. However, this is not a deal-breaker. To make that class a Singleton, you just need to make sure that the constructor is only called once.

So, set up your Singleton like normal:

private static var _instance: DataModel = null;
public static function get instance(): DataModel
{
	return _instance;
}

Then, in the constructor of said Singleton, add the following:

if ( !_instance )
{
	_instance = this;
} else {
	throw new Error( "DataModel is a Singleton: only one instance allowed." );
}

All I’ve done here is move the creation of the Singleton from the get instance() method to the constructor. And if the constructor is called more than once, it throws an error. Now you can instantiate the class through MXML, just like any other component.

<model:DataModel
		id="dataModel"
		xmlURL="fileName.xml" />

Meanwhile in Actionscriptland, you can refer to this instance of your class just like it was a normal Singleton:

	trace( DataModel.instance ); // traces "[object DataModel]"

Plus, if you try to create another instance, be it through MXML or through ActionScript, you’ll get a good ol’ runtime error. You’ll have to wait until runtime to see it, but that’s certainly better than nothing, right?

Discussion

3 comments for “MXML Singletons in Flex 3”

  1. Sorry, I don’t get it? Which part of the source code will be written in an AS file and which part in an MXML file?

    Could you give a file example with file names, please?

    Posted by Christian P. Achter | June 8, 2008, 11:50 am
  2. You can create the Singleton class entirely in AS and then instantiate it using MXML. In this case, DataModel (DataModel.as) is the Singleton, and I instantiated it with a tag in my main MXML document.

    Posted by Zack Jordan | June 17, 2008, 11:35 am
  3. Hey

    I need mxml components which are singleton as well as visual. How can I do that.

    If I base the component on any visual component then I can’t add a constructor, which means i can’t make it singleton.

    Posted by Nishant | October 21, 2008, 1:59 am

Post a comment

Twitterpated