jQuery Plugin Boilerplate


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // A boilerplate for kick-starting jQuery plugins development
  2. // version 1.2, April 29th, 2011
  3. // by Stefan Gabos
  4. // with help from Steven Black, Rob Lifford
  5.  
  6. (function($) {
  7.  
  8. $.fn.pluginName = function(method) {
  9.  
  10. var defaults = {
  11. foo: 'bar'
  12. }
  13.  
  14. var settings = {}
  15.  
  16. var methods = {
  17.  
  18. init : function(options) {
  19. settings = $.extend({}, defaults, options)
  20. return this.each(function() {
  21. var
  22. $element = $(this),
  23. element = this;
  24. // code goes here
  25. });
  26. },
  27.  
  28. foo_public_method: function() {
  29. // code goes here
  30. }
  31.  
  32. }
  33.  
  34. var helpers = {
  35.  
  36. foo_private_method: function() {
  37. // code goes here
  38. }
  39.  
  40. }
  41.  
  42. if (methods[method]) {
  43. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  44. } else if (typeof method === 'object' || !method) {
  45. return methods.init.apply(this, arguments);
  46. } else {
  47. $.error( 'Method "' + method + '" does not exist in pluginName plugin!');
  48. }
  49.  
  50. }
  51.  
  52. })(jQuery);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.