iDatamining.org

I am looking for projects to work on
Please contact with me at yiyu.jia@BostonInfoPro.com!

Saturday, May 28, 2011

Using dynamic class loading feature in ExtJS 4

ExtJS 4 introduces a new feature about dynamical class loading. It is not recommended in a product environment. However, it will be very useful when you want to debug your application. Especially, it is extremely helpful when you have to debug into source code of ExtJS 4 as it is still buggy now. Here is a template that I use in my project.

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,



Wednesday, May 18, 2011

DB2 for i5/OS V5R4 have no buit-in UPSERT support.

Recently, I need to do UPDATE_or_INSERT (UPSERT) in DB2 for i5/OS V5R4. But, this version DB2 does not have built-in UPSERT function.

In MySQL, I have two ways to implement UPSERT. One is using REPLACE INTO. The other is using ON DUPLICATE KEY UPDATE

It seems that MERGE statement can be used for UPSERT purpose though it is designed for different purpose. But, MERGE statement is not support DB2 v8 for i5/OS V5. It requires 6.1 of the OS.

The following is an article about how to write SQL for both DB2 and MySQL. I bookmark it here as reference: Writing SQL for both MySQL and DB2

Wednesday, May 4, 2011

Implementing Singleton object in JavaScript

I read a discussion in ExtJS forum about how to implement private methods (see here). It let me think what the guy wants is a modularization design, which is discussed in "A modular architecture based approach for Single Page Web application development". In order to implement the design, we need to use JavaScript closure concept. Here, I am going to further discuss a little about Singleton in JavaScript.

Singleton in Javascript is a little bit tough to be understood by Java developer because JavaScript does not use "class" to pursue OOP capability. Instead, it uses prototype to implement object inheriting.So, JavaScript has no "class". It only has Object. Each function is an Object in the memory.

Therefore, if you are just developing a project by yourself only, you can certainly implement your singleton as simple as below,
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!

In above examples, we used techniques like self-invoking, constructor function, javaScript closures etc.

Different from other language, JavaScript has no block scope. But, it has function scope. So, we use self-invoking function to add extra layer of scope.

Singpleton is useful to implement Factory design pattern. And, it can be useful if we want to implement function module as singleton.