Skip to content

Commit df190bf

Browse files
committed
add: jquery boilerplate snippet
1 parent 40c088c commit df190bf

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

snippets/js/jquery/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,5 @@
310310
- trim
311311
- type
312312
- unique
313+
- [Others](zothers/)
314+
- jQuery Boilerplate

snippets/js/jquery/zothers/README.md

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

Comments
 (0)