----- Original Message ----- From: dorkie dork from dorktown To: flexcoders@yahoogroups.com Sent: Sunday, January 11, 2009 5:05 AM Subject: Re: [flexcoders]Need help creating a Singleton with MXML
Yay! That's the class I needed! :) To answer your question why. First I like to use MXML. Any tool can parse XML. Plus, with this class, I plan to drop it into all my future project and set properties on it. If I had more than one instance it would screw up the application. Other classes use some of its functionality so I am providing a getInstance(). The problem has been that the other classes may be declared before the MXML instance. So I updated my code to always call the getInstance() method and both problems have been solved!!! Eventually for functionality like that you will end up using actionscript classes, so it would be better to go there now that put off that day. If you are determined to use MXML in this way, there's nothing stopping you implementing the singleton as an actionscript class then having that class instantiate the MXML class and return a reference to it. There's no need to have a situation where you want to use the MXML class but it doesn't exist. Until I saw this thread I had never imagined that anyone would use MXML for creating singletons. Paul On Sat, Jan 10, 2009 at 12:38 AM, Haykel BEN JEMIA <hayke...@gmail.com> wrote: You should probably implement the IMXMLObject interface. Override the initialized method where you can set the class instance (to 'this'). The getInstance() method can create a default class instance if it's null and return it. Don't save references to the class instance in the classes that need to use it. Instead always call getInstance(). This way, until the MXML singleton is initialized, a 'default' singleton will be used. Afterwards, the MXML instance will be used. This said, why do you need the singleton to be MXML? Haykel Ben Jemia Allmas Web & RIA Development http://www.allmas-tn.com On Sat, Jan 10, 2009 at 5:47 AM, dorkie dork from dorktown <dorkiedorkfromdorkt...@gmail.com> wrote: I'm stumped. I'm trying to create a singleton class that is also an MXML class. <managers:SingletonClass /> creates one instance obviously. But another class needs to reference this class. So class 2 calls SingletonClass.getInstance(). It's possible that class 2 will be created before the MXML SingletonClass instance. It's ok for me if there is a ghost instance as long as everything works like it should. As long as the instance that is in the main MXML file is the one thats in effect. Also, when debugging this, the id is always an empty string. I'm extending EventDispatcher. dorkie dork from singleton-town place