|
| 1 | +<snippet> |
| 2 | + <content><![CDATA[ |
| 3 | +// the semi-colon before function invocation is a safety net against concatenated |
| 4 | +// scripts and/or other plugins which may not be closed properly. |
| 5 | +;(function ( $, window, document, undefined ) { |
| 6 | +
|
| 7 | + "use strict"; |
| 8 | +
|
| 9 | + // undefined is used here as the undefined global variable in ECMAScript 3 is |
| 10 | + // mutable (ie. it can be changed by someone else). undefined isn't really being |
| 11 | + // passed in so we can ensure the value of it is truly undefined. In ES5, undefined |
| 12 | + // can no longer be modified. |
| 13 | +
|
| 14 | + // window and document are passed through as local variable rather than global |
| 15 | + // as this (slightly) quickens the resolution process and can be more efficiently |
| 16 | + // minified (especially when both are regularly referenced in your plugin). |
| 17 | +
|
| 18 | + // Create the defaults once |
| 19 | + var pluginName = "defaultPluginName", |
| 20 | + defaults = { |
| 21 | + propertyName: "value" |
| 22 | + }; |
| 23 | +
|
| 24 | + // The actual plugin constructor |
| 25 | + function Plugin ( element, options ) { |
| 26 | + this.element = element; |
| 27 | + // jQuery has an extend method which merges the contents of two or |
| 28 | + // more objects, storing the result in the first object. The first object |
| 29 | + // is generally empty as we don't want to alter the default options for |
| 30 | + // future instances of the plugin |
| 31 | + this.settings = $.extend( {}, defaults, options ); |
| 32 | + this._defaults = defaults; |
| 33 | + this._name = pluginName; |
| 34 | + this.init(); |
| 35 | + } |
| 36 | +
|
| 37 | + // Avoid Plugin.prototype conflicts |
| 38 | + $.extend(Plugin.prototype, { |
| 39 | + init: function () { |
| 40 | + // Place initialization logic here |
| 41 | + // You already have access to the DOM element and |
| 42 | + // the options via the instance, e.g. this.element |
| 43 | + // and this.settings |
| 44 | + // you can add more functions like the one below and |
| 45 | + // call them like the example bellow |
| 46 | + this.yourOtherFunction("jQuery Boilerplate"); |
| 47 | + }, |
| 48 | + yourOtherFunction: function (text) { |
| 49 | + // some logic |
| 50 | + $(this.element).text(text); |
| 51 | + } |
| 52 | + }); |
| 53 | +
|
| 54 | + // A really lightweight plugin wrapper around the constructor, |
| 55 | + // preventing against multiple instantiations |
| 56 | + $.fn[ pluginName ] = function ( options ) { |
| 57 | + return this.each(function() { |
| 58 | + if ( !$.data( this, "plugin_" + pluginName ) ) { |
| 59 | + $.data( this, "plugin_" + pluginName, new Plugin( this, options ) ); |
| 60 | + } |
| 61 | + }); |
| 62 | + }; |
| 63 | +
|
| 64 | +})( jQuery, window, document ); |
| 65 | +]]></content> |
| 66 | + <tabTrigger>jqb</tabTrigger> |
| 67 | + <description>jq - jQuery Boilerplate</description> |
| 68 | +</snippet> |
0 commit comments