Advertisement
Gordon___From

Untitled

Dec 17th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. /*!
  2. * jQuery lightweight plugin boilerplate
  3. * Original author: @ajpiano
  4. * Further changes, comments: @addyosmani
  5. * Licensed under the MIT license
  6. */
  7.  
  8. // the semi-colon before the function invocation is a safety
  9. // net against concatenated scripts and/or other plugins
  10. // that are not closed properly.
  11. ;(function ( $, window, document, undefined ) {
  12.  
  13. // undefined is used here as the undefined global
  14. // variable in ECMAScript 3 and is mutable (i.e. it can
  15. // be changed by someone else). undefined isn't really
  16. // being passed in so we can ensure that its value is
  17. // truly undefined. In ES5, undefined can no longer be
  18. // modified.
  19.  
  20. // window and document are passed through as local
  21. // variables rather than as globals, because this (slightly)
  22. // quickens the resolution process and can be more
  23. // efficiently minified (especially when both are
  24. // regularly referenced in your plugin).
  25.  
  26. // Create the defaults once
  27. var pluginName = 'defaultPluginName',
  28. defaults = {
  29. propertyName: "value"
  30. };
  31.  
  32. // The actual plugin constructor
  33. function Plugin(el, options) {
  34. this.el = el;
  35. this.$el = $(element);
  36.  
  37. // jQuery has an extend method that merges the
  38. // contents of two or more objects, storing the
  39. // result in the first object. The first object
  40. // is generally empty because we don't want to alter
  41. // the default options for future instances of the plugin
  42. this.options = $.extend( {}, defaults, options) ;
  43.  
  44. this._defaults = defaults;
  45. this._name = pluginName;
  46.  
  47. this.init();
  48. this.bindEvents();
  49. }
  50.  
  51. Plugin.prototype = {
  52. init: function () {
  53. // Place initialization logic here
  54. // You already have access to the DOM element and
  55. // the options via the instance, e.g. this.element
  56. // and this.options
  57. },
  58.  
  59. bindEvents: function () {
  60. this.$el.on('click', this.someHandler.bind(this);
  61. },
  62.  
  63. someHandler: function () {
  64. this.$el.on('click.' + pluginName, this.someHandler.bind(this);
  65. }
  66. }
  67.  
  68. // A really lightweight plugin wrapper around the constructor,
  69. // preventing against multiple instantiations
  70. $.fn[pluginName] = function ( options ) {
  71. return this.each(function () {
  72. if (!$.data(this, 'plugin_' + pluginName)) {
  73. $.data(this, 'plugin_' + pluginName,
  74. new Plugin( this, options ));
  75. }
  76. });
  77. }
  78.  
  79. })( jQuery, window, document );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement