First of all, in HTML HEADER, I include ExtJS file ext-dev.js instead of ext-all-debug.js . And, ext-dev.js is the only JavaScript file I need to included there.
Then, in the first page of ExtJS app, I have code like below,
REPLACE INTO. The other is using ON DUPLICATE KEY UPDATE var singleton = {
method1: function(){},
method2: function(){}
}
However, if we want to have a more complex singleton object, which has private variable and method, we may employee below code,var Singleton = (function() {
//a private variable pointing to singleton object.
var _instance = null;
//a private construction function, which is actually
//the singleton object itself.
function PrivateConstructor() {
var publicVar = ``Hello '';
this.publicMethod = function(name) {
alert(publicVar + para);
}
}
//This function return caller an object used
//for getting singleton instance.
return new function() {
this.getInstance = function() {
if (_instance == null) {
_instance = new PrivateConstructor();
_instance.constructor = null; //no more constructor!.
}
return _instance;
}
}
})();
var singletonInstance = Singleton.getInstance();
var singletonInstance2 = Singleton.getInstance();
alert(singletonInstance == singletonInstance2); //display true!
Or, this code give you true singleton too,function singleton() {
var instance = (function() {
var privateVar = "Hello ";
function privateMethod (param) {
alert(privateVar + param);
}
return { // public interface
publicMethod1: function (name) {
privateMethod(name);
}
};
})();
singleton = function () { // re-define the function for subsequent calls
return instance;
};
return new singleton; // call the new function
};
var singletonInstance = new singleton;
var singletonInstance2 = new singleton;
alert(singletonInstance == singletonInstance2); //display true!