|
| 1 | +--- |
| 2 | +chapter : plugins |
| 3 | +section : 2 |
| 4 | +title : How to create a basic plugin |
| 5 | +attribution: jQuery Fundamentals |
| 6 | +--- |
| 7 | +## How to create a basic plugin |
| 8 | + |
| 9 | +The notation for creating a typical plugin is as follows: |
| 10 | + |
| 11 | +<div class="example" markdown="1"> |
| 12 | + (function($){ |
| 13 | + $.fn.myNewPlugin = function() { |
| 14 | + return this.each(function(){ |
| 15 | + // do something |
| 16 | + }); |
| 17 | + }; |
| 18 | + }(jQuery)); |
| 19 | +</div> |
| 20 | + |
| 21 | +Don't let that confuse you though. The point of a jQuery plugin is to extend jQuery's prototype object, and that's what's happening on this line: |
| 22 | + |
| 23 | +<div class="example" markdown="1"> |
| 24 | + $.fn.myNewPlugin = function() { //... |
| 25 | +</div> |
| 26 | + |
| 27 | +We wrap this assignment in an immediately-invoked function: |
| 28 | + |
| 29 | +<div class="example" markdown="1"> |
| 30 | + (function($){ |
| 31 | + //... |
| 32 | + }(jQuery)); |
| 33 | +</div> |
| 34 | + |
| 35 | +This has the effect of creating a "private" scope that allows us to extend jQuery using the dollar symbol without having to risk the possibility that the dollar has been overwritten by another library. |
| 36 | + |
| 37 | +So our actual plugin, thus far, is this: |
| 38 | + |
| 39 | +<div class="example" markdown="1"> |
| 40 | + $.fn.myNewPlugin = function() { |
| 41 | + return this.each(function(){ |
| 42 | + // do something |
| 43 | + }); |
| 44 | + }; |
| 45 | +</div> |
| 46 | + |
| 47 | +The `this` keyword within the new plugin refers to the jQuery object on which the plugin is being called. |
| 48 | + |
| 49 | +<div class="example" markdown="1"> |
| 50 | + var somejQueryObject = $('#something'); |
| 51 | + |
| 52 | + $.fn.myNewPlugin = function() { |
| 53 | + alert(this === somejQueryObject); |
| 54 | + }; |
| 55 | + |
| 56 | + somejQueryObject.myNewPlugin(); // alerts 'true' |
| 57 | +</div> |
| 58 | + |
| 59 | +Your typical jQuery object will contain references to any number of DOM elements, and that's why jQuery objects are often referred to as collections. |
| 60 | + |
| 61 | +So, to do something with a collection we need to loop through it, which is most easily achieved using jQuery's `each()` method: |
| 62 | + |
| 63 | +<div class="example" markdown="1"> |
| 64 | + $.fn.myNewPlugin = function() { |
| 65 | + return this.each(function(){ |
| 66 | + |
| 67 | + }); |
| 68 | + }; |
| 69 | +</div> |
| 70 | + |
| 71 | +jQuery's `each()` method, like most other jQuery methods, returns a jQuery object, thus enabling what we've all come to know and love as 'chaining' (`$(...).css().attr()...`). |
| 72 | +We wouldn't want to break this convention so we return the this object. |
| 73 | +Within this loop you can do whatever you want with each element. |
| 74 | +Here's an example of a small plugin using some of the techniques we've discussed: |
| 75 | + |
| 76 | +<div class="example" markdown="1"> |
| 77 | + (function($){ |
| 78 | + $.fn.showLinkLocation = function() { |
| 79 | + return this.filter('a').each(function(){ |
| 80 | + $(this).append( |
| 81 | + ' (' + $(this).attr('href') + ')' |
| 82 | + ); |
| 83 | + }); |
| 84 | + }; |
| 85 | + }(jQuery)); |
| 86 | + |
| 87 | + // Usage example: |
| 88 | + $('a').showLinkLocation(); |
| 89 | +</div> |
| 90 | + |
| 91 | +This handy plugin goes through all anchors in the collection and appends the href attribute in brackets. |
| 92 | + |
| 93 | +<div class="example" markdown="1"> |
| 94 | + <!-- Before plugin is called: --> |
| 95 | + <a href="page.html">Foo</a> |
| 96 | + |
| 97 | + <!-- After plugin is called: --> |
| 98 | + <a href="page.html">Foo (page.html)</a> |
| 99 | +</div> |
| 100 | + |
| 101 | +Our plugin can be optimized though: |
| 102 | + |
| 103 | +<div class="example" markdown="1"> |
| 104 | + (function($){ |
| 105 | + $.fn.showLinkLocation = function() { |
| 106 | + return this.filter('a').append(function(){ |
| 107 | + return ' (' + this.href + ')'; |
| 108 | + }); |
| 109 | + }; |
| 110 | + }(jQuery)); |
| 111 | +</div> |
| 112 | + |
| 113 | +We're using the `append` method's capability to accept a callback, and the return value of that callback will determine what is appended to each element in the collection. |
| 114 | +Notice also that we're not using the `attr` method to retrieve the href attribute, because the native DOM API gives us easy access with the aptly named href property. |
| 115 | + |
| 116 | +Here's another example of a plugin. |
| 117 | +This one doesn't require us to loop through every element with the `each()` method. |
| 118 | +Instead, we're simply going to delegate to other jQuery methods directly: |
| 119 | + |
| 120 | +<div class="example" markdown="1"> |
| 121 | + (function($){ |
| 122 | + $.fn.fadeInAndAddClass = function(duration, className) { |
| 123 | + return this.fadeIn(duration, function(){ |
| 124 | + $(this).addClass(className); |
| 125 | + }); |
| 126 | + }; |
| 127 | + }(jQuery)); |
| 128 | + |
| 129 | + // Usage example: |
| 130 | + $('a').fadeInAndAddClass(400, 'finishedFading'); |
| 131 | +</div> |
0 commit comments