Singletons and Ext.util.Observable

Singletons and Ext.util.Observable

Blake Anderton
//

A lot of landboss is built on ASP.NET server controls but also contains a lot of javascript code, ExtJS code to be precise.  To get around a lot of the headache synchronizing the server/client relationship we tend to use javascript singletons quite a bit.  We’ll have a singleton for the “Page” object, user controls may have their own singleton, and so on.  We end up using them quite a bit more like namespaces of functions so it ends up working well.  The basic construct for us has been:

Namespace = function(){

   return{

      fn: function(){

         //do something

      }

   };

}();

 

Lately though we’ve wanted to provide some more robust interaction between our page and user control singletons, so we’ve been trying to work out how to best do things like events.  ExtJS has the very cool Overservable interface, but not being very familiar with advanced javascript techniques I wasn’t able to think of how to best use the Ext.extend() function on the singleton.  The answer turns out to be quite simple, but ugly (the story of javascript’s life, unfortunately).

 

Namespace = function(){
   var _singleton = function(){

      this.events = { //my events };

      this.fn = function(){

         //do something

      };

   }

   Ext.extend(_singleton, Ext.util.Observable);

   return new _singleton();

}();

Need Expert Help?

See Our Full Menu of Data Services

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!