-
Notifications
You must be signed in to change notification settings - Fork 3
/
quotes.js
39 lines (33 loc) · 1.01 KB
/
quotes.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
function App () {
this.data = [];
}
App.prototype.init = function init () {
function getHash () { return window.location.hash };
$(window).on('hashchange', function () {
this.render(getHash().split('#!/quote/')[1]);
}.bind(this)).trigger('hashchange');
}
App.prototype.getData = function getData (url, callback) {
var app = this;
$.getJSON(url, function (json) {
this.data = json;
callback.call(this, json);
}.bind(this));
}
App.prototype.render = function render (id) {
id = id || Math.floor(Math.random() * this.data.length);
var quote = this.findQuoteById(id);
var quoteEl = $('#quote');
quoteEl.find('blockquote').html(quote.quote);
quoteEl.find('cite').html(quote.author);
quoteEl.find('.permalink').attr('href', '#!/quote/' + id);
};
App.prototype.findQuoteById = function findQuoteById (id) {
for (var i = 0; i < this.data.length; i++) {
if (id == this.data[i].id) return this.data[i];
}
};
var app = new App();
app.getData('/quotes.json', function () {
this.init();
});