You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page/plugins/advanced_plugin_concepts.md
+32-32Lines changed: 32 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ default plugin settings. This is important because it makes it very easy for
9
9
plugin users to override/customize the plugin with minimal code. And this is
10
10
where we begin to take advantage of the function object.
11
11
12
-
<javascript>
12
+
```
13
13
// plugin definition
14
14
$.fn.hilight = function(options) {
15
15
// Extend our default options with those provided.
@@ -23,25 +23,25 @@ $.fn.hilight.defaults = {
23
23
foreground: 'red',
24
24
background: 'yellow'
25
25
};
26
-
</javascript>
26
+
```
27
27
28
28
Now users can include a line like this in their scripts:
29
29
30
-
<javascript>
30
+
```
31
31
// this need only be called once and does not
32
32
// have to be called from within a 'ready' block
33
33
$.fn.hilight.defaults.foreground = 'blue';
34
-
</javascript>
34
+
```
35
35
36
36
And now we can call the plugin method like this and it will use a blue foreground color:
37
37
38
-
<javascript>
38
+
```
39
39
$('#myDiv').hilight();
40
-
</javascript>
40
+
```
41
41
42
42
As you can see, we've allowed the user to write a single line of code to alter the default foreground color of the plugin. And users can still selectively override this new default value when they want:
43
43
44
-
<javascript>
44
+
```
45
45
// override plugin default foreground color
46
46
$.fn.hilight.defaults.foreground = 'blue';
47
47
// ...
@@ -52,7 +52,7 @@ $('.hilightDiv').hilight();
52
52
$('#green').hilight({
53
53
foreground: 'green'
54
54
});
55
-
</javascript>
55
+
```
56
56
57
57
### Provide public access to secondary functions as applicable
58
58
@@ -62,7 +62,7 @@ implementation of our plugin may define a function called "format" which
62
62
formats the hilight text. Our plugin may now look like this, with the default
63
63
implementation of the format method defined below the hilight function.
And now we can hilight each of these divs uniquely using a single line of script:
189
189
190
-
<javascript>
190
+
```
191
191
$('.hilight').hilight();
192
-
</javascript>
192
+
```
193
193
194
194
###Bob and Sue
195
195
196
196
Let's say Bob has created a wicked new gallery plugin (called "superGallery") which takes a list of images and makes them navigable. Bob's thrown in some animation to make it more interesting. He's tried to make the plugin as customizable as possible, and has ended up with something like this:
The first thing that probably comes to your mind (ok, maybe not the first) is the prospect of how huge this plugin must be to accommodate such a level of customization. The plugin, if it weren't fictional, would probably be a lot larger than necessary. There are only so many kilobytes people will be willing to spend!
232
232
@@ -254,7 +254,7 @@ Developers who use your plugin shouldn't have to learn a new language or termino
254
254
255
255
Bob thought he was offering maximum customization with his *delay* option (look above). He made it so that with his plugin you can specify four different delays, "quite short," "very short," "quite long," or "very long":
256
256
257
-
<javascript>
257
+
```
258
258
var delayDuration = 0;
259
259
switch (settings.delay) {
260
260
case 'very short' : delayDuration = 100;
@@ -267,7 +267,7 @@ switch (settings.delay) {
267
267
break;
268
268
default : delayDuration = 200
269
269
}
270
-
</javascript>
270
+
```
271
271
272
272
Not only does this limit the level of control people have, but it takes up quite a bit of space. Twelve lines of code just to define the delaying time is a bit much, don't you think? A better way to construct this option would be to let plugin users specify the amount of time (in milliseconds) as a number, so that no processing of the option needs to take place.
273
273
@@ -279,7 +279,7 @@ If your plugin creates elements to be used within the DOM, then it's a good idea
@@ -291,7 +291,7 @@ var $wrapper = $('<div />')
291
291
.attr(settings.wrapperAttrs)
292
292
.appendTo(settings.container);
293
293
$wrapper.append('...'); // Easy to reference later...
294
-
</javascript>
294
+
```
295
295
296
296
Notice that we've created a reference to the injected wrapper and we're also calling the 'attr' method to add any specified attributes to the element. So, in our settings it might be handled like this:
297
297
@@ -309,15 +309,15 @@ Notice that we've created a reference to the injected wrapper and we're also cal
309
309
// We can use the extend method to merge options/settings as usual:
310
310
// But with the added first parameter of TRUE to signify a DEEP COPY:
311
311
var settings = $.extend(true, {}, defaults, options);
312
-
</javascript>
312
+
```
313
313
314
314
The *$.extend()* method will now recurse through all nested objects to give us a merged version of both the defaults and the passed options, giving the passed options precedence.
315
315
316
316
The plugin user now has the power to specify any attribute of that wrapper element so if they require that there be a hook for any CSS styles then they can quite easily add a class or change the name of the ID without having to go digging around in plugin source.
317
317
318
318
The same model can be used to let the user define CSS styles:
319
319
320
-
<javascript>
320
+
```
321
321
var defaults = {
322
322
323
323
wrapperCSS : {},
@@ -331,7 +331,7 @@ The same model can be used to let the user define CSS styles:
331
331
.attr(settings.wrapperAttrs)
332
332
.css(settings.wrapperCSS) // ** Set CSS!
333
333
.appendTo(settings.container);
334
-
</javascript>
334
+
```
335
335
336
336
Your plugin may have an associated StyleSheet where developers can add CSS styles. Even in this situation it's a good idea to offer some convenient way of setting styles in JavaScript, without having to use a selector to get at the elements.
337
337
@@ -341,7 +341,7 @@ Your plugin may have an associated StyleSheet where developers can add CSS style
341
341
342
342
If your plugin is driven by events then it might be a good idea to provide a callback capability for each event. Plus, you can create your own custom events and then provide callbacks for those. In this gallery plugin it might make sense to add an 'onImageShow' callback.
343
343
344
-
<javascript>
344
+
```
345
345
var defaults = {
346
346
347
347
onImageShow : function(){}, // we define an empty anonymous function
@@ -366,7 +366,7 @@ If your plugin is driven by events then it might be a good idea to provide a cal
366
366
367
367
Instead of initiating the callback via traditional means (adding parenthesis) we're calling it in the context of 'this' which will be a reference to the image node. This means that you have access to the actual image node through the 'this' keyword within the callback:
368
368
369
-
<javascript>
369
+
```
370
370
$('ul.imgs li').superGallery({
371
371
372
372
onImageShow : function() {
@@ -378,7 +378,7 @@ Instead of initiating the callback via traditional means (adding parenthesis) we
378
378
// ...
379
379
380
380
});
381
-
</javascript>
381
+
```
382
382
383
383
Similarly you could add an "onImageHide" callback and numerous other ones...
0 commit comments