-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
88 lines (76 loc) · 2.38 KB
/
main.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
/**
* Created by momchillgorchev on 29/05/15.
*/
var model = {
init: function(){
this.catList = [
{
'name': 'Boomer',
'score': 0,
'imgUrl': 'http://dreamatico.com/data_images/cat/cat-8.jpg'
},
{
'name': 'Moshi',
'score': 0,
'imgUrl': 'http://www.hdwallpaperscool.com/wp-content/uploads/2013/11/Cute-cat-top-pictures-desktop-wallpapers-full.jpg'
}
];
},
getCatList: function(){
return this.catList;
},
updateCatScore: function(catName){
var cats = model.getCatList();
for(var i = 0; i < cats.length; i++){
if(cats[i].name === catName){
console.log(catName);
cats[i].score++;
}
}
}
};
var view = {
init: function(){
this.catList = document.getElementById('catList');
this.activeArea = document.getElementById('activeArea');
this.activeCat = this.activeArea.querySelector('#activeCat');
this.catScore = this.activeArea.querySelector('#catScore');
view.render()
},
render: function(){
var catList = hub.getCats();
for(var j = 0; j < catList.length; j++){
var catName = document.createElement('li');
catName.innerHTML = catList[j].name;
catName.dataset.imgurl = catList[j].imgUrl;
var currentCat = catList[j];
catName.addEventListener('click', function(e){
view.activeCat.src = e.target.dataset.imgurl;
view.activeCat.dataset.cactive = e.target.innerHTML;
view.catScore.innerHTML = currentCat.score;
});
console.log(catName);
this.catList.appendChild(catName);
}
this.activeCat.addEventListener('click', function(e){
var oldScore = parseInt(view.catScore.innerHTML);
var newScore = oldScore + 1;
view.catScore.innerHTML = newScore;
console.log(newScore);
hub.updateScore(view.activeCat.dataset.cactive);
});
}
};
var hub = {
init: function(){
model.init();
view.init();
},
getCats: function(){
return model.getCatList();
},
updateScore: function(catName){
model.updateCatScore(catName);
}
};
hub.init();