forked from rmurphey/jqfundamentals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
100 lines (79 loc) · 3.34 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Your task is to populate the page with the data the user requests using Ajax. Your code should do the following:
- Use Ajax to load the data in exercises/data/people.json.
- Use the data to create an option element for each person, and add the option to the select element.
- Bind a change event to the select element so that, when it is changed, you request the data for the selected person. Data for each of the people listed in the people.json file is located at exercises/data/people/{id}.json.
- Use the data for the person to place the person's avatar in the #info element.
- Clicking on the avatar should take you to the user's Twitter page.
- Hovering over the avatar should make the following information about the person (from the JSON data) slide down below the avatar:
- their name
- their bio
- The first time data for a person is viewed, use the data about the person to add the text "Latest tweet from {username}" and make it a link to their latest tweet (from the JSON data) to the #seen element.
- Ensure that you only fetch data about a person once; that is, store data once you fetch it so you don't have to fetch it repeatedly.
*/
$(document).ready(function() {
var $select = $('#people select'),
$seen = $('#seen'),
$info = $('#info'),
personDataCache = {};
$('<option/>', { html : 'Select a person' }).appendTo($select);
$.ajax('data/people.json', {
dataType : 'json',
success : function(data) {
$.each(data.people, function(i, p) {
$('<option/>', {
value : p.id,
html : p.name
}).appendTo($select);
});
$select.bind('change', function(e) {
var personId = $(this).val();
if (!personId) { return; }
getPersonData(personId, showPerson);
});
$info.bind('mouseenter', function() {
var $this = $(this);
if ($this.children('div').length) { return; }
var person = $(this).data('person'),
detailTemplate = '<div><p>{{twitter}}</p><p>{{bio}}</p></div>';
if (!person) { return; }
var html = detailTemplate
.replace('{{twitter}}', person.twitter)
.replace('{{bio}}', person.bio || '');
$(html).hide().appendTo(this).slideDown();
});
}
});
function showPerson(person) {
var avatarTemplate = '<a href="http://twitter.com/{{twitter}}"><img src="{{avatar}}" alt="{{twitter}}" /></a>',
html = avatarTemplate
.replace('{{avatar}}', person.avatar)
.replace('{{twitter}}', person.twitter);
$info.html(html).data('person', person);
}
function getPersonData(personId, callback) {
var dfd = $.when(personDataCache[personId] || getRemotePersonData(personId));
dfd.then(callback);
// if there's nothing in the personDataCache
// for the person, then this is the first time
// we've fetched data for this person
if (!personDataCache[personId]) {
dfd.then(function(person) {
personDataCache[personId] = person;
markSeen(person);
});
}
}
function markSeen(person) {
var seenTemplate = '<li><a href="{{tweet}}">Latest tweet from {{twitter}}</a></li>',
html = seenTemplate
.replace('{{tweet}}', person.tweet)
.replace(/{{twitter}}/g, person.twitter);
$seen.append(html);
}
function getRemotePersonData(personId) {
return $.ajax('data/people/' + personId + '.json', {
dataType : 'json'
});
}
});