JavaScript
Article
By Sam Deering

Function Structure with Namespace jQuery Template

By Sam Deering

Hi guys, thought I would share with you the template i use to structure some code when writing a new jQuery function inside the JQUERY4U.POSTS namespace. Not every function (or plugins for that matter) are coded the same way, that’s a fact. But looking at templates is a good way to learn to write efficient code.

--ADVERTISEMENT--

/**
* Function Name
* Function Description
* @author          
* @modifiedby      
* @copyright       
* @version         
* @lastmodified    
* @requires
*/

;JQUERY4U.POSTS = 
{
    name: 'JQUERY4U.POSTS',
	
	//variables available to whole of namespace
	postIds: [],
	
    init: function($, opts)
    {
        if(typeof opts!='undefined')
        {
            $.extend(this.options, opts); 
        }
        
        $(function()
        {  
            JQUERY4U.POSTS.setupPostLayout();
            JQUERY4U.POSTS.setupPostOptions();
	    JQUERY4U.POSTS.setupPostText();
	    JQUERY4U.POSTS.setupPostImage();
	    //etc
            
            // check if IE 6 (use a mix of feature detection and the version string).
            if(!($.support.cssFloat) && ($.browser.version).charAt(0) == '6'){
				JQUERY4U.POSTS.hideSelect=true;
            }
        
            // preload the CSS background
            preloadImage = new Image(); 
            preloadImage.src = "/fcweb/searchpanel/images/autocompleteBg.gif";
        });
    },
	
	setupPostLayout: function(){
		//code
    },
	
	setupPostOptions: function(){
		//code
    },
	
	setupPostText: function(){
		//code
    },
	
	setupPostImage: function(){
		//code
    }
	
	//example call to options
	JQUERY4U.POSTS.options.postimageurl
}
JQUERY4U.POSTS.init(jQuery);
Recommended
Sponsors
The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account