Skip to content

Commit 317ea38

Browse files
committed
Add rest of ajax chapters content
1 parent 1d57b6e commit 317ea38

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
chapter : ajax
3+
section : 6
4+
title : Ajax Events
5+
attribution: jQuery Fundamentals
6+
---
7+
## Ajax Events
8+
9+
Often, you’ll want to perform an operation whenever an Ajax requests starts or stops, such as showing or hiding a loading indicator.
10+
Rather than defining this behavior inside every Ajax request, you can bind Ajax events to elements just like you'd bind other events.
11+
For a complete list of Ajax events, visit [http://docs.jquery.com/Ajax_Events](http://docs.jquery.com/Ajax_Events "Ajax Events documentation on docs.jquery.com").
12+
13+
<div class="example" markdown="1">
14+
Setting up a loading indicator using Ajax Events
15+
16+
$('#loading_indicator')
17+
.ajaxStart(function() { $(this).show(); })
18+
.ajaxStop(function() { $(this).hide(); });
19+
</div>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
chapter : ajax
3+
section : 7
4+
title : Exercises
5+
attribution: jQuery Fundamentals
6+
---
7+
## Exercises
8+
9+
### Load External Content
10+
11+
Open the file `/exercises/index.html` in your browser. Use the file `/exercises/js/load.js`.
12+
Your task is to load the content of a blog item when a user clicks on the title of the item.
13+
14+
1. Create a target div after the headline for each blog post and store a reference to it on the headline element using `$.fn.data`.
15+
16+
2. Bind a click event to the headline that will use the $.fn.load method to load the appropriate content from /exercises/data/blog.html into the target div. Don't forget to prevent the default action of the click event.
17+
18+
Note that each blog headline in index.html includes a link to the post.
19+
You'll need to leverage the href of that link to get the proper content from blog.html.
20+
Once you have the href, here's one way to process it into an ID that you can use as a selector in `$.fn.load`:
21+
22+
<div class="example" markdown="1">
23+
var href = 'blog.html#post1';
24+
var tempArray = href.split('#');
25+
var id = '#' + tempArray[1];
26+
</div>
27+
28+
Remember to make liberal use of `console.log` to make sure you're on the right path!
29+
30+
### Load Content Using JSON
31+
32+
Open the file `/exercises/index.html` in your browser. Use the file `/exercises/js/specials.js`.
33+
Your task is to show the user details about the special for a given day when the user selects a day from the select dropdown.
34+
35+
1. Append a target div after the form that's inside the #specials element; this will be where you put information about the special once you receive it.
36+
37+
2. Bind to the change event of the select element; when the user changes the selection, send an Ajax request to `/exercises/data/specials.json`.
38+
39+
3. When the request returns a response, use the value the user selected in the select (hint: `$.fn.val`) to look up information about the special in the JSON response.
40+
41+
4. Add some HTML about the special to the target div you created.
42+
43+
5. Finally, because the form is now Ajax-enabled, remove the submit button from the form.
44+
45+
Note that we're loading the JSON every time the user changes their selection. How could we change the code so we only make the request once, and then use a cached response when the user changes their choice in the select?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
chapter : ajax
3+
section : 5
4+
title : Working with JSONP
5+
attribution: jQuery Fundamentals
6+
---
7+
## Working with JSONP
8+
9+
The advent of JSONP — essentially a consensual cross-site scripting hack — has opened the door to powerful mashups of content.
10+
Many prominent sites provide JSONP services, allowing you access to their content via a predefined API.
11+
A particularly great source of JSONP-formatted data is the [Yahoo! Query Language](http://developer.yahoo.com/yql/console/), which we'll use in the following example to fetch news about cats.
12+
13+
<div class="example" markdown="1">
14+
Using YQL and JSONP
15+
16+
$.ajax({
17+
url : 'http://query.yahooapis.com/v1/public/yql',
18+
19+
// the name of the callback parameter,
20+
// as specified by the YQL service
21+
jsonp : 'callback',
22+
23+
// tell jQuery we're expecting JSONP
24+
dataType : 'jsonp',
25+
26+
// tell YQL what we want and that we want JSON
27+
data : {
28+
q : 'select title,abstract,url from search.news where query="cat"',
29+
format : 'json'
30+
},
31+
32+
// work with the response
33+
success : function(response) {
34+
console.log(response);
35+
}
36+
});
37+
</div>
38+
39+
jQuery handles all the complex aspects of JSONP behind-the-scenes — all we have to do is tell jQuery the name of the JSONP callback parameter specified by YQL ("callback" in this case), and otherwise the whole process looks and feels like a normal Ajax request.
40+

0 commit comments

Comments
 (0)