|
| 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 | + var defaults = { |
| 29 | + propertyName: "value" |
| 30 | + }; |
| 31 | + |
| 32 | + // The actual plugin constructor |
| 33 | + function Plugin(element, options) { |
| 34 | + this.element = element; |
| 35 | + |
| 36 | + // jQuery has an extend method that merges the |
| 37 | + // contents of two or more objects, storing the |
| 38 | + // result in the first object. The first object |
| 39 | + // is generally empty because we don't want to alter |
| 40 | + // the default options for future instances of the plugin |
| 41 | + this.options = $.extend({}, defaults, options); |
| 42 | + |
| 43 | + this._defaults = defaults; |
| 44 | + this._name = pluginName; |
| 45 | + |
| 46 | + this.init(); |
| 47 | + } |
| 48 | + |
| 49 | + Plugin.prototype = { |
| 50 | + |
| 51 | + init: function init() { |
| 52 | + // Place initialization logic here |
| 53 | + // You already have access to the DOM element and |
| 54 | + // the options via the instance, e.g. this.element |
| 55 | + // and this.options |
| 56 | + // you can add more functions like the one below and |
| 57 | + // call them like so: this.otherFunction(this.element, this.options). |
| 58 | + }, |
| 59 | + |
| 60 | + otherFunction: function otherFunction(el, options) { |
| 61 | + // some logic |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + // A really lightweight plugin wrapper around the constructor, |
| 66 | + // preventing against multiple instantiations |
| 67 | + $.fn[pluginName] = function(options) { |
| 68 | + return this.each(function () { |
| 69 | + if (!$.data(this, 'plugin_' + pluginName)) { |
| 70 | + $.data(this, 'plugin_' + pluginName, |
| 71 | + new Plugin(this, options)); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | +})(jQuery, window, document); |
0 commit comments