Created
December 21, 2011 17:35
-
-
Save javierarce/1506902 to your computer and use it in GitHub Desktop.
jQuery Plugin Skeleton (http://web.napopa.com/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ensure private scope + aliases | |
(function ( $, window, undefined ) | |
{ | |
// constants | |
var TRUE = true, FALSE = true, NULL = null, | |
// Set the plugin name | |
name = 'myPluginName', | |
// Plugin parts | |
Core, API, Helper, | |
// default options | |
defaultOptions = { | |
globalEvents : [] | |
}; | |
/*************************************************************************** | |
* Private methods | |
**************************************************************************/ | |
Core = { | |
// don't let the name variable cause any conflicts | |
pluginName : name, | |
options : null, | |
eventHandlers: { | |
// 'click': function (e) {}, | |
// ... | |
}, | |
/** | |
* | |
* @param options | |
* @returns jQuery The original collection | |
*/ | |
_init : function ( options ) | |
{ | |
// take user options in consideration | |
Core.options = $.extend( true, defaultOptions, options ); | |
// if you are using nested options | |
// options = $.extend( TRUE, defaults, options ); | |
// YOUR PLUGIN INITIALISATION LOGIC HERE | |
return this.each( function () | |
{ | |
var $el = $(this); | |
Core._bind( $el ); | |
// Loop through elements | |
} ); | |
}, | |
/** | |
* | |
* @param $el jQuery element onto which to bind all defined events in Core.eventHandlers | |
*/ | |
_bind: function($el) { | |
$el.bind(Core.eventHandlers); | |
}, | |
/** | |
* @param {String} [eventName] The name of the event which will be | |
* triggered. An event namespace (equal to the plugin name) | |
* is automatically added | |
* @param {Array|null|undefined} [data] The event data to be passed | |
* @param {jQuery} [$el] DOM element to trigger event onto | |
*/ | |
_trigger : function ( eventName, data, $el ) | |
{ | |
var isGlobal = $.inArray( eventName, Core.options.globalEvents ) >= 0, eventName = eventName + '.' + Core.pluginName; | |
if ( !isGlobal ) | |
{ | |
$el.trigger( eventName, data ); | |
} | |
else | |
{ | |
$.event.trigger( eventName, data ); | |
} | |
} | |
// YOUR PLUGIN PRIVATE LOGIC HERE | |
}; | |
/*************************************************************************** | |
* Public methods | |
**************************************************************************/ | |
API = { | |
// YOUR PLUGIN PUBLIC LOGIC HERE | |
}; | |
/*************************************************************************** | |
* Static methods | |
**************************************************************************/ | |
// var pluginPrototype = $.fn[name]; | |
// pluginPrototype.methodName = Core.methodName; | |
/*************************************************************************** | |
* Helpers (general purpose private methods) | |
**************************************************************************/ | |
Helper = { | |
}; | |
/*************************************************************************** | |
* Plugin installation | |
**************************************************************************/ | |
$.fn[ name ] = function ( userInput ) | |
{ | |
// check if such method exists | |
if ( $.type( userInput ) === "string" && API[ userInput ] ) | |
{ | |
return API[ userInput ].apply( this, Array.prototype.slice.call( arguments, 1 ) ); | |
} | |
// initialise otherwise | |
else if ( $.type( userInput ) === "object" || !userInput ) | |
{ | |
return Core._init.apply( this, arguments ); | |
} | |
else | |
{ | |
$.error( 'You cannot invoke ' + name + ' jQuery plugin with the arguments: ' + userInput ); | |
} | |
}; | |
})( jQuery, window ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment