SingletonSampleForFlex2.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application layout="vertical"
- xmlns:mx="http://www.adobe.com/2006/mxml"
- viewSourceURL="srcview/index.html">
- <mx:Script>
- <![CDATA[
- import mx.core.Singleton;
- import org.pepe.managers.IFooManager;
- import org.pepe.managers.IHogeManager;
- private function helloHandler():void
- {
- var cName:String = "org.pepe.managers::HogeManager";
- // This doesn't need.
- // But you can check which Class is registered or not
- var c:Class = Singleton.getClass(cName);
- // get instance
- var hoge:IHogeManager =
- Singleton.getInstance(cName) as IHogeManager;
- hoge.sayHello();
- }
- private function goodByeHandler():void
- {
- var cName:String = "org.pepe.managers::FooManager";
- // This doesn't need.
- // But you can check which Class is registered or not
- var c:Class = Singleton.getClass(cName);
- // get instance
- var foo:IFooManager =
- Singleton.getInstance(cName) as IFooManager;
- foo.sayGoodBye();
- }
- ]]>
- </mx:Script>
- <mx:Label text="Singleton Sample for Flex 2.0.1"
- fontSize="26" fontWeight="bold" />
- <mx:Spacer height="20" />
- <mx:HBox>
- <mx:Button label="Hello" click="helloHandler()" />
- <mx:Button label="GoodBye" click="goodByeHandler()" />
- </mx:HBox>
- </mx:Application>
You have to register classes to Singleton before you use it. About Mixin metadata, See The [Transient] and [Mixin] metadata tags.
org.pepe.managers.AppBootstrap (AS3)
- package org.pepe.managers
- {
- import flash.utils.getDefinitionByName;
- import mx.core.Singleton;
- import mx.managers.ISystemManager;
- import mx.utils.ObjectUtil;
- [Mixin]
- public class AppBootstrap
- {
- // SystemManager call this function
- // before Application initialize
- public static function init(sm:ISystemManager):void
- {
- // set a break point here and try to debug:)
- trace(ObjectUtil.toString(sm.info()));
- // register Class info to Singleton class
- Singleton.registerClass(
- "org.pepe.managers::HogeManager",
- Class(getDefinitionByName("org.pepe.managers::HogeManager"))
- );
- Singleton.registerClass(
- "org.pepe.managers::FooManager",
- Class(getDefinitionByName("org.pepe.managers::FooManager"))
- );
- }
- private var _hoge:HogeManager; // for compiler
- private var _foo:FooManager; // for compiler
- public function AppBootstrap()
- {
- super();
- }
- }
- }
org.pepe.managers.HogeManager (AS3)
- package org.pepe.managers
- {
- import flash.events.EventDispatcher;
- import flash.events.IEventDispatcher;
- import mx.controls.Alert;
- public class FooManager extends EventDispatcher implements IFooManager
- {
- private static var _instance:FooManager;
- // Singleton call this function
- public static function getInstance():FooManager
- {
- if(!_instance){
- _instance = new FooManager();
- }
- return _instance;
- }
- public function FooManager(target:IEventDispatcher=null)
- {
- super(target);
- }
- public function sayGoodBye():void
- {
- Alert.show("GoodBye", "FooManager");
- }
- }
- }
You have to add compiler option like below.
"-load-config+=config/custom-config.xml"
custom-config.xml
<?xml version="1.0"?>
<flex-config xmlns="http://www.adobe.com/2006/flex-config">
<symbol>org.pepe.managers.AppBootstrap</symbol>
</includes>
</flex-config>
<flex-config xmlns="http://www.adobe.com/2006/flex-config">
<symbol>org.pepe.managers.AppBootstrap</symbol>
</includes>
</flex-config>
SingletonSampleForFlex2
source of SingletonSampleForFlex2
SingletonSampleForFlex3
source of SingletonSampleForFlex3
BTW, on Japanese environment, css definition of source view is output in Japanese. Therefore, source view doesn't work correctly.
