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);

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.

  • http://www.echteinfach.tv/ Ben

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

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

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