-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
133 lines (118 loc) · 4.39 KB
/
app.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* app.js
*
* This is our RSS feed reader application. It uses the Google
* Feed Reader API to grab RSS feeds as JSON object we can make
* use of. It also uses the Handlebars templating library and
* jQuery.
*/
// The names and URLs to all of the feeds we'd like available.
var allFeeds = [
{
name: 'Udacity Blog',
url: 'http://blog.udacity.com/feed'
}, {
name: 'CSS Tricks',
url: 'http://feeds.feedburner.com/CssTricks'
}, {
name: 'HTML5 Rocks',
url: 'http://feeds.feedburner.com/html5rocks'
}, {
name: 'Linear Digressions',
url: 'http://feeds.feedburner.com/udacity-linear-digressions'
}
];
/* This function starts up our application. The Google Feed
* Reader API is loaded asynchonously and will then call this
* function when the API is loaded.
*/
function init() {
// Load the first feed we've defined (index of 0).
loadFeed(0);
}
/* This function performs everything necessary to load a
* feed using the Google Feed Reader API. It will then
* perform all of the DOM operations required to display
* feed entries on the page. Feeds are referenced by their
* index position within the allFeeds array.
* This function all supports a callback as the second parameter
* which will be called after everything has run successfully.
*/
function loadFeed(id, cb) {
var feedUrl = allFeeds[id].url,
feedName = allFeeds[id].name;
$.ajax({
type: "POST",
url: 'https://rsstojson.udacity.com/parseFeed',
data: JSON.stringify({url: feedUrl}),
contentType:"application/json",
success: function (result, status){
var container = $('.feed'),
title = $('.header-title'),
entries = result.feed.entries,
entriesLen = entries.length,
entryTemplate = Handlebars.compile($('.tpl-entry').html());
title.html(feedName); // Set the header text
container.empty(); // Empty out all previous entries
/* Loop through the entries we just loaded via the Google
* Feed Reader API. We'll then parse that entry against the
* entryTemplate (created above using Handlebars) and append
* the resulting HTML to the list of entries on the page.
*/
entries.forEach(function(entry) {
container.append(entryTemplate(entry));
});
if (cb) {
cb();
}
},
error: function (result, status, err){
//run only the callback without attempting to parse result due to error
if (cb) {
cb();
}
},
dataType: "json"
});
}
/* Google API: Loads the Feed Reader API and defines what function
* to call when the Feed Reader API is done loading.
*/
google.setOnLoadCallback(init);
/* All of this functionality is heavily reliant upon the DOM, so we
* place our code in the $() function to ensure it doesn't execute
* until the DOM is ready.
*/
$(function() {
var container = $('.feed'),
feedList = $('.feed-list'),
feedItemTemplate = Handlebars.compile($('.tpl-feed-list-item').html()),
feedId = 0,
menuIcon = $('.menu-icon-link');
/* Loop through all of our feeds, assigning an id property to
* each of the feeds based upon its index within the array.
* Then parse that feed against the feedItemTemplate (created
* above using Handlebars) and append it to the list of all
* available feeds within the menu.
*/
allFeeds.forEach(function(feed) {
feed.id = feedId;
feedList.append(feedItemTemplate(feed));
feedId++;
});
/* When a link in our feedList is clicked on, we want to hide
* the menu, load the feed, and prevent the default action
* (following the link) from occurring.
*/
feedList.on('click', 'a', function() {
var item = $(this);
$('body').addClass('menu-hidden');
loadFeed(item.data('id'));
return false;
});
/* When the menu icon is clicked on, we need to toggle a class
* on the body to perform the hiding/showing of our menu.
*/
menuIcon.on('click', function() {
$('body').toggleClass('menu-hidden');
});
}());