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();
}();