Method Overloading in JQuery

This is a code script which I did while I was helping out a friend in his project. This is a pretty neat implementation of having overloaded methods in JQuery. For this to work I utilized a sweet method which John Resig wrote as given below:


// addMethod - By John Resig (MIT Licensed)
function addMethod(object, name, fn){
    var old = object[ name ];
    object[ name ] = function(){
        if ( fn.length == arguments.length )
            return fn.apply( this, arguments );
        else if ( typeof old == 'function' )
            return old.apply( this, arguments );
    };
}

Given below is how I utilized the addMethod of Johns to have a overloaded method implementation:


function UploadFileWithData() {
    addMethod(this, "upload", function(oframe, FileDescription, List){
       if (document.getElementById(oframe) != null) {

            ifUpload = document.getElementById(oframe);
            ifUpload.contentWindow.document.getElementById("hiddenDescription").value = FileDescription;
            ifUpload.contentWindow.document.getElementById("hiddenFilesList").value = List;
            ifUpload.contentWindow.document.getElementById("hiddenIsEditRequired").value = "0";
            ifUpload.contentWindow.document.getElementById("btnSubmit").click();
        }
        var oSection = document.getElementById("hSection");

        if (oSection != null) {

            oSection.value = List;
        }
    }
    );
    addMethod(this, "upload", function(oframe, FileDescription, List, AddEdit,Id, IsFileDeleted){
        if (document.getElementById(oframe) != null)
        {

        ifUpload = document.getElementById(oframe);
        ifUpload.contentWindow.document.getElementById("hiddenDescription").value = FileDescription;
        ifUpload.contentWindow.document.getElementById("hiddenFilesList").value = List;
        ifUpload.contentWindow.document.getElementById("hiddenIsEditRequired").value = "1";
        ifUpload.contentWindow.document.getElementById("hiddenSId").value = Id;
        ifUpload.contentWindow.document.getElementById("hiddenIsFileDeleted").value = IsFileDeleted;
        ifUpload.contentWindow.document.getElementById("btnSubmit").click();

        }
        var oSection = document.getElementById("hSection");

        if (oSection != null) {

            oSection.value = List;
        }
    }
    );
}

Hope it helps.

Happy Programming!!!

Cheers,

Raja

Advertisements

0 Responses to “Method Overloading in JQuery”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: