Skip to content

Commit 04ccc94

Browse files
committed
Changed code blocks in plugins to markdown ticks
1 parent 5160f6b commit 04ccc94

File tree

3 files changed

+80
-80
lines changed

3 files changed

+80
-80
lines changed

page/plugins/advanced_plugin_concepts.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ default plugin settings. This is important because it makes it very easy for
99
plugin users to override/customize the plugin with minimal code. And this is
1010
where we begin to take advantage of the function object.
1111

12-
<javascript>
12+
```
1313
// plugin definition
1414
$.fn.hilight = function(options) {
1515
// Extend our default options with those provided.
@@ -23,25 +23,25 @@ $.fn.hilight.defaults = {
2323
foreground: 'red',
2424
background: 'yellow'
2525
};
26-
</javascript>
26+
```
2727

2828
Now users can include a line like this in their scripts:
2929

30-
<javascript>
30+
```
3131
// this need only be called once and does not
3232
// have to be called from within a 'ready' block
3333
$.fn.hilight.defaults.foreground = 'blue';
34-
</javascript>
34+
```
3535

3636
And now we can call the plugin method like this and it will use a blue foreground color:
3737

38-
<javascript>
38+
```
3939
$('#myDiv').hilight();
40-
</javascript>
40+
```
4141

4242
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:
4343

44-
<javascript>
44+
```
4545
// override plugin default foreground color
4646
$.fn.hilight.defaults.foreground = 'blue';
4747
// ...
@@ -52,7 +52,7 @@ $('.hilightDiv').hilight();
5252
$('#green').hilight({
5353
foreground: 'green'
5454
});
55-
</javascript>
55+
```
5656

5757
### Provide public access to secondary functions as applicable
5858

@@ -62,7 +62,7 @@ implementation of our plugin may define a function called "format" which
6262
formats the hilight text. Our plugin may now look like this, with the default
6363
implementation of the format method defined below the hilight function.
6464

65-
<javascript>
65+
```
6666
6767
// plugin definition
6868
$.fn.hilight = function(options) {
@@ -80,7 +80,7 @@ $.fn.hilight = function(options) {
8080
$.fn.hilight.format = function(txt) {'
8181
return '<strong>' + txt + '</strong>';
8282
};
83-
</javascript>
83+
```
8484

8585
We could have just as easily supported another property on the options object
8686
that allowed a callback function to be provided to override the default
@@ -100,11 +100,11 @@ that's where this type of extensibility is useful. The Cycle Plugin exposes a
100100
"transitions" object to which users can add their own custom transition
101101
definitions. It's defined in the plugin like this:
102102

103-
<javascript>
103+
```
104104
$.fn.cycle.transitions = {
105105
// ...
106106
};
107-
</javascript>
107+
```
108108

109109
This technique makes it possible for others to define and ship transition definitions that plug-in to the Cycle Plugin.
110110

@@ -124,7 +124,7 @@ function will log the number of selected elements to the Firebug console. To
124124
create a closure, we wrap the entire plugin definition in a function (as
125125
detailed in the jQuery Authoring Guidelines).
126126

127-
<javascript>
127+
```
128128
// create closure
129129
(function($) {
130130
// plugin definition
@@ -140,7 +140,7 @@ detailed in the jQuery Authoring Guidelines).
140140
// ...
141141
// end of closure
142142
})(jQuery);
143-
</javascript>
143+
```
144144

145145
Our "debug" method cannot be accessed from outside of the closure and thus is private to our implementation.
146146

@@ -152,7 +152,7 @@ even more powerful. Personally, I love the Metadata Plugin because it lets you
152152
use unobtrusive markup to override plugin options (which is particularly useful
153153
when creating demos and examples). And supporting it is very simple!
154154

155-
<javascript>
155+
```
156156
// plugin definition
157157
$.fn.hilight = function(options) {
158158
// ... build main options before element iteration
@@ -164,15 +164,15 @@ $.fn.hilight = function(options) {
164164
var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
165165
166166
//...
167-
</javascript>
167+
```
168168

169169
<div class="note" markdown="1">
170170
This line is added as the last argument to *jQuery.extend* so it will override
171171
any other option settings. Now we can drive behavior from the markup if we
172172
choose:
173173
</div>
174174

175-
<markup>
175+
```
176176
<!-- markup -->
177177
<div class="hilight { background: 'red', foreground: 'white' }">
178178
Have a nice day!
@@ -183,19 +183,19 @@ choose:
183183
<div class="hilight { background: 'green' }">
184184
Have a nice day!
185185
</div>
186-
</markup>
186+
```
187187

188188
And now we can hilight each of these divs uniquely using a single line of script:
189189

190-
<javascript>
190+
```
191191
$('.hilight').hilight();
192-
</javascript>
192+
```
193193

194194
###Bob and Sue
195195

196196
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:
197197

198-
<javascript>
198+
```
199199
jQuery.fn.superGallery = function(options) {
200200
// Bob's default settings:
201201
var defaults = {
@@ -226,7 +226,7 @@ jQuery.fn.superGallery = function(options) {
226226
// ----------------------------
227227
});
228228
};
229-
</javascript>
229+
```
230230

231231
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!
232232

@@ -254,7 +254,7 @@ Developers who use your plugin shouldn't have to learn a new language or termino
254254

255255
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":
256256

257-
<javascript>
257+
```
258258
var delayDuration = 0;
259259
switch (settings.delay) {
260260
case 'very short' : delayDuration = 100;
@@ -267,7 +267,7 @@ switch (settings.delay) {
267267
break;
268268
default : delayDuration = 200
269269
}
270-
</javascript>
270+
```
271271

272272
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.
273273

@@ -279,7 +279,7 @@ If your plugin creates elements to be used within the DOM, then it's a good idea
279279

280280
A bad implementation:
281281

282-
<javascript>
282+
```
283283
// Plugin code
284284
$('&lt;div id="the_gallery_Wrapper" /&gt;').appendTo('body');
285285
$('#the_gallery_wrapper').append('...');
@@ -291,7 +291,7 @@ var $wrapper = $('&lt;div /&gt;')
291291
.attr(settings.wrapperAttrs)
292292
.appendTo(settings.container);
293293
$wrapper.append('...'); // Easy to reference later...
294-
</javascript>
294+
```
295295

296296
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:
297297

@@ -309,15 +309,15 @@ Notice that we've created a reference to the injected wrapper and we're also cal
309309
// We can use the extend method to merge options/settings as usual:
310310
// But with the added first parameter of TRUE to signify a DEEP COPY:
311311
var settings = $.extend(true, {}, defaults, options);
312-
</javascript>
312+
```
313313
314314
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.
315315
316316
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.
317317
318318
The same model can be used to let the user define CSS styles:
319319
320-
<javascript>
320+
```
321321
var defaults = {
322322

323323
wrapperCSS : {},
@@ -331,7 +331,7 @@ The same model can be used to let the user define CSS styles:
331331
.attr(settings.wrapperAttrs)
332332
.css(settings.wrapperCSS) // ** Set CSS!
333333
.appendTo(settings.container);
334-
</javascript>
334+
```
335335
336336
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.
337337
@@ -341,7 +341,7 @@ Your plugin may have an associated StyleSheet where developers can add CSS style
341341
342342
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.
343343
344-
<javascript>
344+
```
345345
var defaults = {
346346

347347
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
366366

367367
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:
368368

369-
<javascript>
369+
```
370370
$('ul.imgs li').superGallery({
371371
372372
onImageShow : function() {
@@ -378,7 +378,7 @@ Instead of initiating the callback via traditional means (adding parenthesis) we
378378
// ...
379379
380380
});
381-
</javascript>
381+
```
382382

383383
Similarly you could add an "onImageHide" callback and numerous other ones...
384384

0 commit comments

Comments
 (0)