-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (67 loc) · 2.1 KB
/
index.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
$(function() {
var $tvShowsContainer = $('#app-body').find('.tv-shows');
$tvShowsContainer.on('click','button.like',function (ev){
var $this = $(this);
/*$this.animate({
'font-size':'30px'
},'slow');*/
$this.closest('.tv-show').toggleClass('liked');
})
function renderShows(shows) {
$tvShowsContainer.find('.loader').remove();
shows.forEach(function (show) {
var article = template
.replace(':name:', show.name)
.replace(':img:', show.image ? show.image.medium : '')
.replace(':summary:', show.summary)
.replace(':img alt:', show.name + " Logo")
var $article = $(article)
$tvShowsContainer.append($article.fadeIn(2000));
})
}
/**
* Submit serach form
*/
$('#app-body')
.find('form')
.submit(function (ev) {
ev.preventDefault();
var busqueda = $(this)
.find('input[type="text"]')
.val();
$tvShowsContainer.find('.tv-show').remove()
var $loader = $('<div class="loader">');
$loader.appendTo($tvShowsContainer);
$.ajax({
url: 'http://api.tvmaze.com/search/shows',
data: { q: busqueda },
success: function (res, textStatus, xhr) {
$loader.remove();
var shows = res.map(function (el) {
return el.show;
})
renderShows(shows);
}
})
})
var template = '<article class="tv-show">' +
'<div class="left img-container">' +
'<img src=":img:" alt=":img alt:">' +
'</div>' +
'<div class="right info">' +
'<h1>:name:</h1>' +
'<p>:summary:</p>' +
'<button class="like">💖</button>' +
'</div>' +
'</article>';
if (!localStorage.shows) {
$.ajax('http://api.tvmaze.com/shows')
.then(function (shows) {
$tvShowsContainer.find('.loader').remove();
localStorage.shows = JSON.stringify(shows);
renderShows(shows);
})
} else {
renderShows(JSON.parse(localStorage.shows));
}
})