Tuesday, 11 December 2012

OOP JS


Running example for me to learn

Running example http://jsfiddle.net/cpoDesign/Cze3X/6/

Creating OOP for js can be done as following.

var masterPage ={
    confirmBoxMsg: 'Selection not available.',  
    settings:{
        version:0.1,        
        gridSettings:{
            width: 'auto',
            height:'auto'
        }
    },
   
    showAlert: function(){    
        alert('test');
    },
    showAlertWithAttr: function(variable){
            alert(variable);
    },
    showAlertWithFromSettings: function(){
        alert('Reading version:'+ this.settings.version);
    },
    dataAccess:function(url,arguments, complete, errorEvent){
        $.ajax({
            url:url,
            type:'GET',
            data:arguments,
            success: complete,          
            error:errorEvent,
        });
    }   
}

// calls logic in printing
//masterPage.showAlert();
    
// logic with wariable
//masterPage.showAlertWithAttr('variable value');

// logic to read variables from object
masterPage.showAlertWithFromSettings()
    
// using ajax call to do some work (not working yet)
    //masterPage.dataAccess("http://www.google.co.uk/",function(res){
    //alert('loaded' + res.responseText);},function(){alert('failed');});