1
- jQuery PluginCreator v2.0.0
2
- ============================
1
+ # jQuery PluginCreator v2.0.0
3
2
4
3
A jQuery plugin for creating stateful, extensible jQuery plugins using ES6
5
4
6
5
7
- Contents
8
- --------
6
+ ## Contents
9
7
10
8
1 . Introduction
11
9
2 . Why v2.x?
12
10
3 . Requirements
13
11
4 . Usage
14
- 5 . The jQueryPlugin class
15
- 6 . The jQuery.addPlugin API
16
- 7 . The jQuery.fn.yourPlugin API
12
+ 5 . The jQuery.addPlugin API
13
+ 6 . The jQuery.fn.yourPlugin API
14
+ 7 . The jQueryPlugin class
17
15
8 . Tests
18
16
19
17
20
- Introduction
21
- ------------
18
+ ## Introduction
22
19
23
- jQuery PluginCreator is a small JavaScript library that can be used in conjunction with jQuery to easily
24
- create jQuery plugins.
20
+ jQuery PluginCreator is a small JavaScript library that can be used in
21
+ conjunction with jQuery to easily create jQuery plugins.
25
22
26
- Creating a plugin with PluginCreator is pretty easy, you simply implement your plugin as an ES6 class
27
- extending the ` jQueryPlugin ` class exported by PluginCreator. PluginCreator creates a new jQuery plugin
28
- function that can be executed against jQuery selections to instantiate the class against selected elements.
23
+ Creating a plugin with PluginCreator is pretty easy, you simply implement
24
+ your plugin as an ES6 class extending the ` jQueryPlugin ` class exported
25
+ by PluginCreator. PluginCreator creates a new jQuery plugin function that
26
+ can be executed against jQuery selections to instantiate the class against
27
+ selected elements.
29
28
30
- Plugins created using PluginCreator can also be extended using standard ES6 inheritance semantics to
31
- implement new plugins that extend functionality in the base plugin.
29
+ Plugins created using PluginCreator can also be extended using standard
30
+ ES6 inheritance semantics to implement new plugins that extend functionality
31
+ in the base plugin.
32
32
33
33
34
- Why v2.x?
35
- ---------
34
+ ## Why v2.x?
36
35
37
- v2.x of jQuery PluginCreator was initiated in order to simplify the project and leverage the simplified
38
- inheritance scheme provided by ES6.
36
+ v2.x of jQuery PluginCreator was initiated in order to simplify the project
37
+ and leverage the simplified inheritance scheme provided by ES6.
39
38
40
- v1.x implemented a custom single-inheritance scheme along with a number of additional features that allowed
41
- for some more complex behaviours including post-definition patching of plugin members and plugin instance
42
- members. This scheme was implemented using the ` esprima ` library and, as a whole, worked fairly well. It
43
- has seen production usage and generally does the job, albeit with a few caveats.
39
+ v1.x implemented a custom single-inheritance scheme along with a number
40
+ of additional features that allowed for some more complex behaviours including
41
+ post-definition patching of plugin members and plugin instance members.
42
+ This scheme was implemented using the ` esprima ` library and, as a whole,
43
+ worked fairly well. It has seen production usage and generally does the
44
+ job, albeit with a few caveats.
44
45
45
- Going forward, however, the desire was to reduce the amount of custom implementation code and capacity to
46
- engage in _ funny business_ while also bringing the project as a whole closer to the ES6 way of doing things.
46
+ Going forward, however, the desire was to reduce the amount of custom
47
+ implementation code and capacity to engage in * funny business* while also
48
+ bringing the project as a whole closer to the ES6 way of doing things.
47
49
Thus, v2.x was born.
48
50
49
51
50
- Requirements
51
- ------------
52
+ ## Requirements
52
53
53
54
jQuery PluginCreator can be used in any of the following JavaScript environments:
54
55
@@ -57,14 +58,13 @@ jQuery PluginCreator can be used in any of the following JavaScript environments
57
58
* Browser + CommonJS
58
59
* Browser + ES6 Modules
59
60
60
- In order to make use of jQuery PluginCreator you will need jQuery. For a browser environment, any recent version should
61
- do the trick.
61
+ In order to make use of jQuery PluginCreator you will need jQuery. For a
62
+ browser environment, any recent version should do the trick.
62
63
63
64
64
- Usage
65
- -----
65
+ ## Usage
66
66
67
- # Browser
67
+ #### Browser
68
68
``` html
69
69
70
70
<html >
@@ -92,7 +92,7 @@ $.addPlugin(myPlugin, {
92
92
93
93
```
94
94
95
- # Browser + AMD
95
+ #### Browser + AMD
96
96
``` javascript
97
97
98
98
define ([" jquery" , " jquery.plugincreator" ], function ($ , pluginCreator ) {
@@ -110,7 +110,7 @@ define(["jquery", "jquery.plugincreator"], function ($, pluginCreator) {
110
110
111
111
` ` `
112
112
113
- # Browser + CommonJS
113
+ #### Browser + CommonJS
114
114
` ` ` javascript
115
115
116
116
var $ = require (" jquery" ),
@@ -129,7 +129,7 @@ $.addPlugin(myPlugin, {
129
129
130
130
` ` `
131
131
132
- # Browser + ES6 Modules
132
+ #### Browser + ES6 Modules
133
133
` ` ` javascript
134
134
135
135
import addPlugin , { jQueryPlugin } from " jquery.plugincreator" ;
@@ -145,4 +145,147 @@ addPlugin(myPlugin, {
145
145
defaultSomething2: 10
146
146
});
147
147
148
- ` ` `
148
+ ` ` `
149
+
150
+
151
+ ## The jQuery.addPlugin API
152
+
153
+ jQuery PluginCreator extends the global jQuery object with the following function:
154
+
155
+ ##### addPlugin(pluginClass, defaults={})
156
+ The addPlugin function accepts two parameters, one of which is optional.
157
+
158
+ The first parameter must be a class that inherits from the *jQueryPlugin*
159
+ class, the second is an optional plain object of default values to be
160
+ available on the ` options` member of instances of the class plugin.
161
+
162
+ When called, addPlugin generates a new function attached to the
163
+ ` jQuery .fn ` object. This function is attached using the ` name` property
164
+ provided by the plugin class. Thus, a plugin class defined as
165
+ ` class myPlugin extends jQueryPlugin {}` will be bound to ` jQuery .fn .myPlugin `
166
+ when passed in to the ` addPlugin` function.
167
+
168
+ ##### addPlugin.jQueryPlugin
169
+ To aid development in environments that don't support ES6 modules, the
170
+ ` jQueryPlugin` class is also made available as a property of the ` addPlugin`
171
+ function that is bound to ` jQuery` .
172
+
173
+
174
+ ## The jQuery.fn.yourPlugin API
175
+
176
+ Once the ` jQuery .addPlugin ` function has been used to attach a new plugin,
177
+ that plugin can be accessed as normal using the ` jQuery .fn .NAME ` object
178
+ and applied to jQuery selections using the standard ` jQuery (" selector" ).NAME ()`
179
+ method:
180
+
181
+ ##### jQuery.fn.NAME(options, ...args)
182
+
183
+ ###### options
184
+ A ` string` or plain ` object` .
185
+
186
+ ###### args
187
+ Additional parameters may be passed to ` jQuery .fn .NAME ` and will be passed on to the plugin processing logic and
188
+ from there to any plugin instance member functions or constructors called.
189
+
190
+ ###### Function behaviour
191
+ The base plugin function which can be used to instantiate plugin
192
+ instances or interact with existing plugin instances.
193
+
194
+ When ` jQuery .fn .NAME ` is called on a given jQuery selection it does the following:
195
+
196
+ 1. If the selection contains exactly 1 element, it returns the result of
197
+ executing the plugin processing logic on that element. This allows a
198
+ call to like ` jQuery (" #your-element" ).yourPlugin (" getInstance" )` to
199
+ work as expected. In instance where a call like ` jQuery (" #your-element" ).yourPlugin (" yourMethod" )`
200
+ would return no value or return the ` undefined ` value then the return
201
+ value will be the jQuery selection, preserving the jQuery chaining effect.
202
+ 2. If the selection does not contain exactly 1 element and...
203
+
204
+ a. ` options === " map" ` , it applies the plugin processing logic to the
205
+ selection using the ` map` operation, returning
206
+ the resultant selection. This output selection can be converted to
207
+ a standard ` Array ` by applying the ` get` operation on the selection.
208
+ When applying the plugin processing logic the initial ` options` value
209
+ of ` " map" ` is discarded. The next argument is considered to be the
210
+ ` options` value and any further arguments are treated as additional parameters.
211
+
212
+ b. ` options !== " map" ` , it applies the plugin processing logic to the
213
+ selection using the ` each` operation, returning the selection as expected.
214
+
215
+ The plugin processing logic does the following:
216
+
217
+ 1. Attempt to retrieve plugin instance associated with input element.
218
+ 2. If an instance is found and ` options` is a ` string` and ` instance[options]`
219
+ is a function, treat the call to ` jQuery .fn .NAME ` as an attempt to call
220
+ a member function on the plugin instance. The member function, ` instance.[options]`
221
+ is called and any additional parameters supplied to ` jQuery .fn .NAME `
222
+ will be passed to the member function being called. If ` options` is not
223
+ a ` string` or ` instance[options]` is not a function, a ` jQueryPluginCreatorError`
224
+ exception will be thrown.
225
+ 3. If no instance is found, instantiate a plugin instance on the element
226
+ using the contents of the ` options` parameter to override values supplied
227
+ by ` jQuery .fn .NAME .defaults ` to the plugin instance. Additionally, any
228
+ additional parameters supplied to ` jQuery .fn .NAME ` will be passed in
229
+ to the ` init` member function of the plugin instance. The plugin instance
230
+ is associated with its parent element using a data attribute of the form
231
+ ` data- jquery- plugincreator- NAME ` . The instantiated plugin is returned,
232
+ allowing plugin instantiation on single-element selections to be used for assignments.
233
+
234
+
235
+ ##### jQuery.fn.NAME.defaults
236
+ The ` defaults` supplied to ` addPlugin` . This is exposed in order to allow
237
+ the key-value pairs stored to manipulated.
238
+
239
+
240
+ ## The jQueryPlugin class
241
+
242
+ The jQueryPlugin class provides a base for stateful jQuery Plugins. The
243
+ following methods are provided by the jQueryPlugin class:
244
+
245
+ ##### constructor(element, defaults={}, options={})
246
+ The default constructor performs a number of important set-up tasks
247
+ for a plugin instance.
248
+
249
+ It binds ` this .element ` to ` element` , ` this .context ` to ` jQuery (element)`
250
+ and ` this .options ` to the result of mering ` options` over ` defaults` .
251
+
252
+ While it is possible to override the ` constructor ` method, it is not
253
+ recommended. Rather, implement your initialization code in the `init`
254
+ method. If you must override `constructor `, ensure you call the
255
+ ancestor `constructor ` to preserve initialisation behaviour.
256
+
257
+ ##### init ()
258
+ The default `init` method does nothing. If you wish to perform
259
+ custom initialization it should be implemented by overriding this
260
+ method.
261
+
262
+ Although the default method accepts no parameters you can provide
263
+ ones when you override it.
264
+
265
+ ##### getInstance()
266
+ The `getInstance` method exists in order to allow for jQuery usage
267
+ such as `let pluginInstance = jQuery("#something").myPlugin("getInstance")`.
268
+
269
+ ##### update(options)
270
+ This method can be used to updated the data stored in `this.options` for
271
+ a plugin instance. The `jQuery.extend` function is used to perform this
272
+ update with the recursive merge option enabled.
273
+
274
+ ##### destroy()
275
+ The `destroy` method is essentially a destructor, albeit one that needs
276
+ to be manually called. The default implementation does the following:
277
+
278
+ 1. Triggers an event named `jquery-plugincreator-NAME.destroy` on `this.context`
279
+ 2. Removes the `jquery-plugincreator-NAME` class on `this.element`
280
+ 3. Removes the `jquery-plugincreator-NAME` data associated with `this.element`
281
+ 4. Removes the `data-jquery-plugincreator-NAME` attribute on `this.element`
282
+
283
+ If you override this method, you should ensure you call through to the
284
+ ancestor `destroy` function to ensure it the method continues behave as
285
+ expected by users.
286
+
287
+
288
+ ## Tests
289
+
290
+ In order to run the tests you will need to checkout the project source,
291
+ execute `npm install` in the source root and then run `npm run-script test`.
0 commit comments