Skip to content

Commit 5a50bbd

Browse files
committed
moving /performance /faq /code-organization code examples to md
1 parent 5160f6b commit 5a50bbd

14 files changed

+76
-73
lines changed

page/code-organization/beware-anonymous-functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ attribution: jQuery Fundamentals
66
Anonymous functions bound everywhere are a pain. They're difficult to debug,
77
maintain, test, or reuse. Instead, use an object literal to organize and name
88
your handlers and callbacks.
9-
<javascript>
9+
```
1010
// BAD
1111
$(document).ready(function() {
1212
$('#magic').click(function(e) {
@@ -37,4 +37,4 @@ var PI = {
3737
};
3838
3939
$(document).ready(PI.onReady);
40-
</javascript>
40+
```

page/code-organization/concepts.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ some concepts that are common to all good code organization patterns.
2323
- Despite jQuery's DOM-centric nature, JavaScript applications are not all
2424
about the DOM. Remember that not all pieces of functionality need to — or
2525
should — have a DOM representation.
26-
- Units of functionality should be [loosely coupled](http://en.wikipedia.org/wiki/Loose_coupling), that is,
26+
- Units of functionality should be [loosely coupled](http://en.wikipedia.org/wiki/Loose_coupling), that is,
2727
a unit of functionality should be able to exist on its own, and communication between
2828
units should be handled via a messaging system such as custom events or
2929
pub/sub. Stay away from direct communication between units of functionality
@@ -45,7 +45,8 @@ doesn't offer any privacy for properties or methods, but it's useful for
4545
eliminating anonymous functions from your code, centralizing configuration
4646
options, and easing the path to reuse and refactoring.
4747

48-
<javascript caption="An object literal">
48+
```
49+
// An object literal
4950
var myFeature = {
5051
myProperty : 'hello',
5152
@@ -66,7 +67,7 @@ options, and easing the path to reuse and refactoring.
6667
myFeature.myMethod(); // logs 'hello'
6768
myFeature.init({ foo : 'bar' });
6869
myFeature.readSettings(); // logs { foo : 'bar' }
69-
</javascript>
70+
```
7071

7172
The object literal above is simply an object assigned to a variable. The object
7273
has one property and several methods. All of the properties and methods are
@@ -77,7 +78,7 @@ be called before the object is functional.
7778
How would we apply this pattern to jQuery code? Let's say that we had this code
7879
written in the traditional jQuery style:
7980

80-
<javascript>
81+
```
8182
// clicking on a list item loads some content
8283
// using the list item's ID and hides content
8384
// in sibling list items
@@ -97,7 +98,7 @@ written in the traditional jQuery style:
9798
);
9899
});
99100
});
100-
</javascript>
101+
```
101102

102103
If this were the extent of our application, leaving it as-is would be fine. On
103104
the other hand, if this was a piece of a larger application, we'd do well to
@@ -106,7 +107,8 @@ want to move the URL out of the code and into a configuration area. Finally, we
106107
might want to break up the chain to make it easier to modify pieces of the
107108
functionality later.
108109

109-
<javascript caption="Using an object literal for a jQuery feature">
110+
```
111+
// Using an object literal for a jQuery feature
110112
var myFeature = {
111113
init : function(settings) {
112114
myFeature.config = {
@@ -166,7 +168,7 @@ functionality later.
166168
};
167169
168170
$(document).ready(myFeature.init);
169-
</javascript>
171+
```
170172

171173
The first thing you'll notice is that this approach is obviously far longer
172174
than the original -- again, if this were the extent of our application, using an
@@ -194,8 +196,8 @@ The module pattern overcomes some of the limitations of the object literal,
194196
offering privacy for variables and functions while exposing a public API if
195197
desired.
196198

197-
<javascript caption="The module pattern">
198-
199+
```
200+
// The module pattern
199201
var feature =(function() {
200202
201203
// private variables and functions
@@ -224,7 +226,7 @@ desired.
224226
feature.sayPrivateThing();
225227
// logs 'secret' and changes the value
226228
// of privateThing
227-
</javascript>
229+
```
228230

229231
In the example above, we self-execute an anonymous function that returns an
230232
object. Inside of the function, we define some variables. Because the variables
@@ -243,7 +245,8 @@ Below is a revised version of the previous example, showing how we could create
243245
the same feature using the module pattern while only exposing one public method
244246
of the module, `showItemByIndex()`.
245247

246-
<javascript caption="Using the module pattern for a jQuery feature">
248+
```
249+
// Using the module pattern for a jQuery feature
247250
$(document).ready(function() {
248251
var feature = (function() {
249252
var $items = $('#myFeature li'),
@@ -297,4 +300,4 @@ of the module, `showItemByIndex()`.
297300
298301
feature.showItemByIndex(0);
299302
});
300-
</javascript>
303+
```

page/code-organization/dont-repeat-yourself.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ attribution: jQuery Fundamentals
44
---
55
Don't repeat yourself; if you're repeating yourself, you're doing it wrong.
66

7-
<javascript>
7+
```
88
// BAD
99
if ($eventfade.data('currently') != 'showing') {
1010
$eventfade.stop();
@@ -25,4 +25,4 @@ $.each($elems, function(i,elem) {
2525
elem.stop();
2626
}
2727
});
28-
</javascript>
28+
```

page/faq/add_keyboard_navigation.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Adding Keyboard Navigation
3-
attribution: Remy Sharp
3+
attribution: Remy Sharp
44
status: needswork
55
editrequired: 2
66
source: http://jqueryfordesigners.com/adding-keyboard-navigation/
@@ -34,17 +34,17 @@ Then a keyboard key is pressed the event break into three separate events, which
3434

3535
So I’m going to use the last phase of the events and use the keyup event.
3636

37-
<div class="example" markdown="1">
37+
```
3838
$(document.documentElement).keyup(function (event) {
3939
// handle cursor keys
4040
});
41-
</div>
41+
```
4242

4343
Note that all event handlers in jQuery, such as click, keyup, mouseover, etc receive an event [argument](http://en.wikipedia.org/wiki/Parameter_(computer_science)) - which I’ve captured under the variable name event.
4444

4545
The left and right key codes are 37 and 39 respectively. So we only want to action the slide if these keys are pressed, so we’ll check the keyCode property on the event - this property is [very well supported in browsers](http://www.quirksmode.org/js/keys.html):
4646

47-
<div class="example" markdown="1">
47+
```
4848
$(document.documentElement).keyup(function (event) {
4949
// handle cursor keys
5050
if (event.keyCode == 37) {
@@ -53,15 +53,15 @@ The left and right key codes are 37 and 39 respectively. So we only want to acti
5353
// go right
5454
}
5555
});
56-
</div>
56+
```
5757

5858
##Triggering the Click on the Right Link
5959

6060
Triggering a click event is easy, but the problem we have to solve is finding the right link to click. Since this slider adds a class of current to the currently active link, we’ll use that as our anchor point, and try to find the link before (if the left cursor key is pressed) and after (if the right cursor key is pressed).
6161

6262
In some cases the current class might be on list element, which would make the navigating slightly easier, but in this case the markup looks a bit like this:
6363

64-
<div class="example" markdown="1">
64+
```
6565
&lt;div class="coda-slider-wrapper"&gt;
6666
&lt;ul&gt;
6767
&lt;li&gt;&lt;a class="current" href="#1"&gt;1&lt;/a&gt;&lt;/li&gt;
@@ -71,31 +71,31 @@ In some cases the current class might be on list element, which would make the n
7171
&lt;li&gt;&lt;a href="#5"&gt;5&lt;/a&gt;&lt;/li&gt;
7272
&lt;/ul&gt;
7373
&lt;/div&gt;
74-
</div>
74+
```
7575

7676
In the case above, the “next” link is #2. Currently there’s no previous available, but we’ll let jQuery handle this for us.
7777

7878
To find the next link, first we need to grab the link that’s currently selected:
7979

80-
<div class="example" markdown="1">
80+
```
8181
$('.coda-slider-wrapper ul a.current')
82-
</div>
82+
```
8383

8484
Next we need to move up the tree (to the li element) and move to the next element and then back down to the a element to trigger a click.
8585

86-
<div class="example" markdown="1">
86+
```
8787
$('.coda-slider-wrapper ul a.current')
8888
.parent() // moves up to the li element
8989
.next() // moves to the adjacent li element
9090
.find('a') // moves down to the link
9191
.click(); // triggers a click on the next link
92-
</div>
92+
```
9393

9494
Since jQuery continues to allow chained methods to work even if there’s no elements found, we can also use prev() in the place of next() when current is on the first element and the code will work just fine still.
9595

9696
Let’s plug this code in to the keyboard event handler:
9797

98-
<div class="example" markdown="1">
98+
```
9999
$(document.documentElement).keyup(function (event) {
100100
// handle cursor keys
101101
if (event.keyCode == 37) {
@@ -111,11 +111,11 @@ Let’s plug this code in to the keyboard event handler:
111111
$('.coda-slider-wrapper ul a.current').parent().next().find('a').click();
112112
}
113113
});
114-
</div>
114+
```
115115

116116
This code can be simplified since a lot of it is repetitive aside from the ‘next’ and ‘prev’. If you’re happy for slightly more complicated code, then use the code below, otherwise stick with the code above.
117117

118-
<div class="example" markdown="1">
118+
```
119119
$(document.documentElement).keyup(function (event) {
120120
var direction = null;
121121
@@ -132,7 +132,7 @@ This code can be simplified since a lot of it is repetitive aside from the ‘ne
132132
$('.coda-slider-wrapper ul a.current').parent()[direction]().find('a').click();
133133
}
134134
});
135-
</div>
135+
```
136136

137137
##Where does this all go?
138138

@@ -142,15 +142,15 @@ Since we’re going in after the slider plugin is run, so it should go *directly
142142

143143
In Mathieu’s code, he had the following:
144144

145-
<div class="example" markdown="1">
145+
```
146146
$().ready(function() {
147147
$('#coda-slider-1').codaSlider();
148148
});
149-
</div>
149+
```
150150

151151
Note that $().ready is the same as $(document).ready (though it’s not a style I would personally use). *Directly* after the plugin (i.e. before the });) results in the following code:
152152

153-
<div class="example" markdown="1">
153+
```
154154
$().ready(function() {
155155
$().ready(function() {
156156
$('#coda-slider-1').codaSlider();
@@ -172,7 +172,7 @@ $().ready(function() {
172172
}
173173
});
174174
});
175-
</div>
175+
```
176176

177177
You can see this all [in action in the demo](http://static.jqueryfordesigners.com/demo/keyboard-nav.html), and be sure to use the left and right keyboard cursor keys to see the demo working.
178178

page/faq/enable_the_back_button.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ This process was our original code, and most of it needs to stay in place. The c
3434

3535
So we listen for the hashchange event, just like we might listen for a click event:
3636

37-
<div class="example" markdown="1">
37+
```
3838
$(window).bind('hashchange', function(){
3939
//do some magic
4040
});
41-
</div>
41+
```
4242

4343
Now we move all of the original code from the click handler in to this new event listener. I’ve copied this code in, and marked in bold which lines we’ll need to change:
4444

45-
<div class="example" markdown="1">
45+
```
4646
$(window).bind('hashchange', function(){
4747
tabContainers.hide();
4848
tabContainers.filter(this.hash).show();
4949
$('div.tabs ul.tabNavigation a').removeClass('selected');
5050
$(this).addClass('selected');
5151
return false;
5252
});
53-
</div>
53+
```
5454

5555
At this point all the references to this need to change, because in our original version this referred to the link that had be clicked. Now we need to determine the link based on the window URL. We can get the newly navigated URL using window.location. This is an object that represents part of the URL, and as such, gives as just the hash by itself:window.location.hash. This is good.
5656

@@ -60,9 +60,9 @@ Next we need to address how we can target the appropriate tab to add the selecte
6060

6161
Since we don’t have this to target the right tab, we need to find the element using jQuery. Since we have the hash, we can use this to find the tab link. We can use the “hash” attribute selector like this:
6262

63-
<div class="example" markdown="1">
63+
```
6464
$('a[hash=#first]')
65-
</div>
65+
```
6666

6767
Therefore we can substitute our hash variable in to the ‘#first’ part of the selector above, and now we’ll have the right element to add the selected class.
6868

@@ -74,7 +74,7 @@ Then one last change to make to our code - we need to trigger the hashchange eve
7474

7575
Our final code looks like this:
7676

77-
<div class="example" markdown="1">
77+
```
7878
$(function() {
7979
var tabContainers = $('div.tabs > div');
8080
tabContainers.hide().filter(':first').show();
@@ -89,6 +89,6 @@ $(window).bind('hashchange', function(){
8989
9090
$(window).trigger("hashchange");
9191
});
92-
</div>
92+
```
9393

9494
Once last note: if we trigger the hashchange event, and there’s no hash on the URL, then we need to give it a default. In this case I’ve given it #first as a default so that it always lands on the first tab if there’s nothing pre-selected.

page/performance/append-outside-loop.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ tags: performance
77
Touching the DOM comes at a cost; if you're appending a lot of elements to the
88
DOM, you will want to append them all at once, rather then one at a time. This is common problem when appending elements within a loop.
99

10-
<javascript>
10+
```
1111
$.each(myArray, function(i, item) {
1212
var newListItem = '<li>' + item + '</li>';
1313
$('#ballers').append(newListItem);
1414
});
15-
</javascript>
15+
```
1616

1717
One common technique is to leverage a document fragment. During each iteration
1818
of the loop, you append the element to the fragment rather than the DOM
1919
element. After the loop, just append the fragment to the DOM element.
2020

21-
<javascript>
21+
```
2222
var frag = document.createDocumentFragment();
2323
2424
$.each(myArray, function (i, item) {
@@ -29,18 +29,18 @@ $.each(myArray, function (i, item) {
2929
});
3030
3131
$('#ballers')[0].appendChild(frag);
32-
</javascript>
32+
```
3333

3434
Another technique, which is quite simple, is to build up a string during each iteration of the loop. After the loop, just set the html of the DOM element to that string.
3535

36-
<javascript>
36+
```
3737
var myHtml = '';
3838
3939
$.each(myArray, function(i, item) {
4040
myHtml += '<li>' + item + '</li>';
4141
});
4242
4343
$('#ballers').html(myHtml);
44-
</javascript>
44+
```
4545

4646
There are of course other techniques you could certainly test out; a great way to test the performance of these is through a site called [jsperf](http://jsperf.com). This site allows you to benchmark each technique and visually see how it performs across all the browsers.

page/performance/cache-loop-length.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ tags: performance
66

77
In a for loop, don't access the length property of an array every time; cache
88
it beforehand.
9-
<javascript>
9+
10+
```
1011
var myLength = myArray.length;
1112
1213
for (var i = 0; i < myLength; i++) {
1314
// do stuff
1415
}
15-
</javascript>
16+
```

0 commit comments

Comments
 (0)