jQuery plugin boilderplate


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



Copy this code and paste it in your HTML
  1. ;(function($) {
  2.  
  3. $.pluginName = function(el, options) {
  4.  
  5. var defaults = {
  6. propertyName: 'value',
  7. onSomeEvent: function() {}
  8. },
  9.  
  10. plugin = this;
  11.  
  12. plugin.settings = {};
  13.  
  14. var init = function() {
  15. plugin.settings = $.extend({}, defaults, options);
  16. plugin.el = el;
  17. plugin.$el = $(el);
  18. // code goes here
  19. };
  20.  
  21. plugin.fooPublicMethod = function() {
  22. // code goes here
  23. };
  24.  
  25. var fooPrivateMethod = function() {
  26. // code goes here
  27. };
  28.  
  29. init();
  30.  
  31. }
  32.  
  33. })(jQuery);
  34.  
  35.  
  36. // Uages
  37.  
  38.  
  39. $(document).ready(function() {
  40.  
  41. // create a new instance of the plugin
  42. var myplugin = new $.pluginName($('#element'), {option: "value"});
  43.  
  44. // call a public method
  45. myplugin.fooPublicMethod();
  46.  
  47. // get the value of a public property
  48. myplugin.settings.property;
  49.  
  50. });

URL: http://jqueryboilerplate.com/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.