Return to Snippet

Revision: 47624
at June 11, 2011 18:13 by realpeterz


Initial Code
/* inspired by iNettus tutorial http://net.tutsplus.com/tutorials/javascript-ajax/inettuts/ */

var JqAppName = {

    jQuery: $,
    
    settings: {
        defaults:{},
        individual:{
            elemId1: {},
            elemId2: {}
        }
    },
    
    init: function(){
       this.method1();
       this.method2();
       this.method3();
    },
    
    getElemSettings: function(arg){ // pass in either an element ID or an object literal containing options to override settings.default
        var $ = this.jQuery, settings = this.settings;
        
        if ( typeof(arg) === "undefined" )
        {  return settings.defaults; }
        
        else if ( typeof(arg) === "string" )
        {
            return (arg&&settings.individuals[arg]) ? $.extend({}, settings.defaults, settings.individuals[arg]) : settings.defaults; 
        } 
        else if ( typeof(arg) === "object" )
        {
            return $.extend({}, settings.defaults, args );
        }
        
    },
    
    method1: function(){
         var JqAppName = this, $ = this.jQuery, settings = this.settings;
         // do something simple without messing with individual settings
    }    
    
    method2: function(id){ // pass in an element id to get its pre-set from settings.individual.elemIdX
         var JqAppName = this, $ = this.jQuery, settings = this.settings;
         var thisElemSettings = typeof(id) === 'undefined' ? JqAppName.getElemSettings('elemId1') : JqAppName.getElemSettings(id);
         // do something awesome
    },
    
    method3: function(options){ // pass an an object for options to override settings.default
         var JqAppName = this, $ = this.jQuery, settings = this.settings;
         var thisElemSettings = typeof(options) === 'undefined' ? JqAppName.getElemSettings({/* options hash */}) : JqAppName.getElemSettings(options); 
         
        // do something awesomer
    }
    

} // end of JqApp scope

Initial URL

                                

Initial Description

                                

Initial Title
jQuery app with custom namespace

Initial Tags
javascript, jquery

Initial Language
JavaScript