06 January 2011

Writing Object oriented Javascript Programming Using DOJO

Writing custom classes using DOJO
writing a simple class
 

> dojo.provide("com.test.Utils");


this will denote which class we are creating.

dojo.declare("com.test.utils, null,
{
   var1 : val1,
   ..

   constructor : function()
   {
     // Initialize the class variable here ..
   }

   // Writing class functions
   function1 : function ()
   {

   }
});


Note this class must be placed under dojo/com/test/ directory and name of js file should be Utils.js. Dojo loads class cosidering dojo as base directory.

Sample class: This class provides some utility functions
// file name: com/test/Utils.js 


dojo.provide('com.test.Utils');


dojo.declare('com.test.Utils', null,
{
   // Initialize the class variables
    constructor : function(ds)
    {
        this.oneByte = 8;

        this.oneKB   = 1024;
        this.oneMB   = 1024 * 1024;
       
this.oneGB   = 1024 * 1024 * 1024;
       
this.oneTB   = 1024 * 1024 * 1024 * 1024;
        this.onePB   = 1024 * 1024 * 1024 * 1024 * 1024;
    },

    // Converts the supplied bytes in Words
    sizeInWord : function(size)
    {
        try {
           size = parseInt(size);
        } catch (e) {
       
    return "-";        }
        if (size == undefined)
       
    return "-";
   
    if (size < this.oneKB) {
       
    return Math.round(size/this.oneByte) + " Bytes";
   
    }
   
    if (size < this.oneMB) {
       
    return Math.round(size/this.oneKB) + " KB";
   
    }
   
    if (size < this.oneGB) {
        
    return Math.round(size/this.oneMB) + " MB";
   
    }
   
    if (size < this.oneTB) {
       
    return Math.round(size/this.oneGB) + " GB";
        }

   
    if (size < this.onePB) {
       
    return Math.round(size/this.oneTB) + " TB";
   
    }

  
      return "INFINITE";
    },

    // convert the provided array to string
    arrToStr: function(input)
    {
   
    if (input == undefined)
       
    return "";

        var str = "";
   
    if (dojo.isArray(input)) {
       
    for (var i = 0; i < input.length; i++) {
       
        str += this.escapeHTMLCodes(input[i]);
            }       
   
        return str;
        }   
        return "";
    },

    // escapes the HTML characters
    escapeHTMLCodes: function (str)
    {
   
    if (str == "" || str == undefined)
       
    return "";

   
    str = str.replace(/&/g, "&amp ;").
   
    str = str.replace(/>/g, "&gt ;");
   
    str = str.replace(/</g, "&lt ;")
   
    str = str.replace(/"/g, "\"");
   
    return str;
    },

    // format the given date in required format
    formatDate: function(date, dateFormat)
    {
   
    var fdate;
   
    if (dateFormat == undefined)
       
    return "-";
   
    try {
       
    fDate = dojo.date.locale.format(new Date(date),
                       {datePattern: dateFormat});
        } catch (e) {
   
        cosole.error(e);
       
    return "-";
        }
   
    return fDate;
    },

   // format the given date in given date and time format
    formatDateAndTime : function(date, dateFormat, timeFormat)
    {
   
    var fdate;
   
    if (dateFormat == undefined)
       
    return "-";
   
    try {
       
    fDate = dojo.date.locale.format(new Date(date),
           
    {datePattern: dateFormat, timePattern: timeFormat});
        } catch (e) {
   
        cosole.error(e);
       
    return "-";
        }
        return fDate;
    },

    // Trim the string
    trimToLength : function(str, start, len)
    {
   
    if (str == undefined || str == "")
       
    return "";
   
    return str.substring(start, len);   
    }
});



How to Use the class in HTML page:

1. Load the module
   dojo.require("com.test.Utils");

2. Using the class

   dojo.addOnLoad(function() {

      //create instance of the class
      var utils = new com.test.Utils();
      alert(utils.sizeInWords(10240)); // show 1 MB
      alert(utils.formatDate(new Date(), "dd MMMM yyyy")
     // will show current date in dd MMMM yyyy format
       ...
   });

No comments:

Post a Comment