Skip to content

Commit 8cfc46d

Browse files
committed
added jquery patterns
1 parent 7025034 commit 8cfc46d

File tree

8 files changed

+135
-0
lines changed

8 files changed

+135
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
// antipattern
10+
var arms = $('div.robotarm', '#container');
11+
// preferred
12+
var arms = $('#container').find('div.robotarm');
13+
14+
// antipattern
15+
$('.reply_form', $(this).closest('.comment')).hide();
16+
// preferred
17+
$('this').closet('.comment').find('.reply_form').hide();
18+
</script>
19+
</body>
20+
</html>

jquery-patterns/data.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
// antipattern
10+
$(elem).data(key,value);
11+
12+
// preferred
13+
$.data(elem,key,value);
14+
</script>
15+
</body>
16+
</html>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/*
10+
better to descend from an id
11+
*/
12+
13+
// antipattern
14+
var arms = $('.container div.robotarm');
15+
16+
// better
17+
var arms = $('#container div.robotarm');
18+
19+
// preferred
20+
var arms = $('#container').find('div.robotarm');
21+
</script>
22+
</body>
23+
</html>

jquery-patterns/detach.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/*
10+
take element off the DOM while manipulating them
11+
*/
12+
var table = $('#some-table');
13+
var parent = table.parent();
14+
15+
table.detach();
16+
table.addLotsAndLotsOfRows();
17+
parent.append(table);
18+
</script>
19+
</body>
20+
</html>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
// antipattern
10+
$('a.trigger', $('#container')[0]).live('click', handlerFn)
11+
12+
// preferred
13+
$('#container').delegate('click', 'a.trigger', handlerFn)
14+
</script>
15+
</body>
16+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/*
10+
specific on the right, light on the left
11+
*/
12+
13+
// antipattern
14+
$('div.data .brad')
15+
16+
// preferred
17+
$('.data td.brad')
18+
</script>
19+
</body>
20+
</html>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
/*
10+
no need to be over-specific
11+
*/
12+
13+
// antipattern
14+
var arms = $('.data table.attendees td.brad');
15+
16+
// preferred
17+
var arms = $('.data td.brad');
18+
</script>
19+
</body>
20+
</html>
File renamed without changes.

0 commit comments

Comments
 (0)