Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e53a5be

Browse files
committed
added basic and extend
1 parent 3e7222d commit e53a5be

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

jquery-plugin-patterns/basic.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/*!
10+
* jQuery basic plugin boilerplate
11+
*/
12+
13+
/*
14+
The most basic form of jQuery plugin
15+
This is great for compactness
16+
*/
17+
18+
$.fn.myPluginName = function() {
19+
// your plugin logic
20+
};
21+
22+
23+
/*
24+
A better foundation to build on
25+
Here, we’ve wrapped our plugin logic in an anonymous function. To ensure that our use of the $ sign as a shorthand creates no conflicts between jQuery and other JavaScript libraries, we simply pass it to this closure, which maps it to the dollar sign, thus ensuring that it can’t be affected by anything outside of its scope of execution.
26+
*/
27+
28+
(function( $ ){
29+
$.fn.myPluginName = function() {
30+
// your plugin logic
31+
};
32+
})( jQuery );
33+
34+
35+
// References
36+
// http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
37+
</script>
38+
</body>
39+
</html>

jquery-plugin-patterns/extend.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/*!
10+
* jQuery extend-based plugin boilerplate
11+
* Author: @oscargodson
12+
* Further changes: @timmywil
13+
* Licensed under the MIT license
14+
*/
15+
16+
/*
17+
As you'll notice below, we're making use of $.fn.extend to create our plugin rather
18+
than opting for $.fn.pluginname. This type of structure may be useful if you need
19+
to add a relatively large number of methods to your plugin. There are however alternatives
20+
to this that may be better suited, including Alex Sexton's prototypal inheritence pattern
21+
which is also included in this repo.
22+
*/
23+
24+
25+
//the semi colon before function invocation is a safety net against concatenated
26+
//scripts and/or other plugins which may not be closed properly.
27+
;(function($){
28+
$.fn.extend({
29+
pluginname: function( options ) {
30+
31+
this.defaultOptions = {};
32+
33+
var settings = $.extend({}, this.defaultOptions, options);
34+
35+
return this.each(function() {
36+
37+
var $this = $(this);
38+
39+
});
40+
41+
}
42+
43+
});
44+
45+
})(jQuery);
46+
47+
48+
// References
49+
// http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
50+
</script>
51+
</body>
52+
</html>

0 commit comments

Comments
 (0)