JavaScript
Article

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.

/**
* 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);
  • http://www.echteinfach.tv/ Ben

    Thanks. This is the first template I have ever seen for developing jquery plugins. Great idea and useful!

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.