Posted By

Japh on 04/27/11


Tagged

plugin jquery boilerplate


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Japh


jQuery Plugin Boilerplate


 / Published in: jQuery
 

URL: http://stefangabos.ro/jquery/jquery-plugin-boilerplate/

This plugin is the work of Stefan Gabos, all credit, rights, and praise to be directed to him here: http://stefangabos.ro/jquery/jquery-plugin-boilerplate/

  1. //
  2. // A jQuery plugin template
  3. //
  4. // Author: Stefan Gabos
  5. //
  6. // Website: http://stefangabos.ro/
  7. //
  8. // Changelog
  9. //
  10. // 2011-04-25 1.0 - Initial release
  11. //
  12. // Read more about jQuery plugin authoring at
  13. // http://docs.jquery.com/Plugins/Authoring
  14. //
  15.  
  16.  
  17. // remember to change every instance of "pluginName" to the name of your plugin!
  18. // read more about plugin authoring at
  19. (function($) {
  20.  
  21. // here it goes!
  22. $.fn.pluginName = function(method) {
  23.  
  24. // plugin's default options
  25. var options = {
  26.  
  27. foo: 'bar'
  28.  
  29. }
  30.  
  31. // public methods
  32. // to keep the $.fn namespace uncluttered, collect all of the plugin's methods in an object literal and call them
  33. // by passing the string name of the method to the plugin
  34. //
  35. // public methods can be called as
  36. // $(selector).pluginName('methodName', arg1, arg2, ... argn)
  37. // where "pluginName" is the name of your plugin and "methodName" is the name of a function available in the
  38. // "methods" object below; arg1 ... argn are arguments to be passed to the method
  39. //
  40. // or, from within the plugin itself, as
  41. // methods.methodName(arg1, arg2, ... argn)
  42. // where "methodName" is the name of a function available in the "methods" object below
  43. var methods = {
  44.  
  45. // this the constructor method that gets called when the object is created
  46. init : function(config) {
  47.  
  48. // iterate through all the DOM elements we are attaching the plugin to
  49. return this.each(function() {
  50.  
  51. // if custom configuration options are provided, update default values
  52. if (config) { $.extend(options, config) }
  53.  
  54. // "element" holds the jQuery object of the current DOM element
  55. var element = $(this);
  56.  
  57. // code goes here
  58.  
  59. });
  60.  
  61. },
  62.  
  63. // a public method. for demonstration purposes only - remove it!
  64. foo_public_method: function() {
  65.  
  66. // code goes here
  67.  
  68. }
  69.  
  70. }
  71.  
  72. // private methods
  73. // these methods can be called only from within the plugin
  74. //
  75. // private methods can be called as
  76. // helpers.methodName(arg1, arg2, ... argn)
  77. // where "methodName" is the name of a function available in the "helpers" object below; arg1 ... argn are
  78. // arguments to be passed to the method
  79. var helpers = {
  80.  
  81. // a private method. for demonstration purposes only - remove it!
  82. foo_private_method: function() {
  83.  
  84. // code goes here
  85.  
  86. }
  87.  
  88. }
  89.  
  90. // if a method as the given argument exists
  91. // (ignore the "init" method which should not be called manually)
  92. if (methods[method] && method.toLowerCase() != 'init') {
  93.  
  94. // call the respective method
  95. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  96.  
  97. // if an object is given as method OR nothing is given as argument
  98. } else if (typeof method === 'object' || !method) {
  99.  
  100. // call the initialization method
  101. return methods.init.apply(this, arguments);
  102.  
  103. // otherwise
  104. } else {
  105.  
  106. // trigger an error
  107. $.error( 'Method "' + method + '" does not exist in PluginName plugin!');
  108.  
  109. }
  110.  
  111. }
  112.  
  113. })(jQuery);

Report this snippet  

You need to login to post a comment.