Working on creating my own plugin and realized that I would very likely end up doing this again in the future. Unfortunately, I have an extremely short memory when writing code. I frequently have to re-read the code I've created the day prior so I can get back on track with what I was doing. As such, I'm a fervent commenter in my code.
So, I'm creating a jQuery plugin framework with comments for everything that's going on in the framework to prevent my brain from having to re-learn everything in a month or so when I do another one.
What I would like help with from the community is for the plugin-savvy folks to take a look at this code and help me fill in the blanks and make corrections to those places where I'm way off base. As you'll likely notice rather quickly, this is taken from the jQuery plugin tooltip construct found here:
http://docs.jquery.com/Plugins/Authoring
Thanks in advance for the help!
- (function( $ ){
- var methods = {
- init : function( options ) { //default method called from the plugin
- var defaults = {
- // place default settings for plugin here
- }
-
- var options = $.extend(defaults, options); // combines any passed-in variables with the default variables and overwrites matching defaults
- return this.each(function(){ // ensures chainability of the plugin
-
- var $this = $(this),
- data = $this.data('PLUGINNAME'); // ????
-
- // If the plugin hasn't been initialized yet
- if ( ! data ) { // ???? not sure how this can't be initialized as we just did this
-
- /*
- Do more setup stuff here
- */
-
- }
- });
- },
-
- destroy : function( ) { // called using $.PLUGINNAME('destroy')
- return this.each(function(){ // ensures chainability
- var $this = $(this),
- data = $this.data('PLUGINNAME'); // ????
- // Namespacing FTW
- $(window).unbind('.PLUGINNAME'); // remove bound actions from window events like resize ????
- data.PLUGINNAME.remove(); // remove the plugin element from the DOM ????
- $this.removeData('PLUGINNAME'); // removes associated data from the object
- })
- },
-
-
- update : function( content ) { // example method. called using $.PLUGINNAME('update')
-
- return this.each(function() { // ensures chainability
-
- //do your update stuff here
-
- })
-
- }
-
- };
- $.fn.PLUGINNAME = function( method ) {
-
- if ( methods[method] ) { // if the called method exists in the methods array
- return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); // apply the passed-in arguments to the method and return result
- } else if ( typeof method === 'object' || ! method ) { // ????
- return methods.init.apply( this, arguments ); // apply passed-in arguments to the init method and return result
- } else {
- $.error( 'Method ' + method + ' does not exist on jQuery.PLUGINNAME' ); // method passed in does not exist in methods array
- }
-
- };
- })( jQuery );