-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.html
126 lines (118 loc) · 3.21 KB
/
final.html
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
<html>
<head>
<title>Vue Test</title>
<!-- Include semantic, my personal favourite front end framework -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.13/semantic.min.css" media="screen" rel="stylesheet" type="text/css">
<style>
#vue_demo {
padding-top: 10%;
padding-left: 10%;
}
.red {
color:#ff0000;
}
img {
width:100px;
height:100px;
margin-right:20px!important;
}
</style>
</head>
<body>
<div id="vue_demo">
<h2 class="ui header">hotels</h2>
<input type="checkbox" v-model="filterSale"> Filter Sale
<div class="ui cards">
<hotel
v-for="hotel in hotels"
v-if="!filterSale || (filterSale && hotel.sale.price != null)"
:hotel="hotel"
:key="hotel.id">
</hotel>
</div>
</div>
</body>
<!-- Include the Vue library -->
<script src="https://unpkg.com/vue@2.5.13/dist/vue.js"></script>
<script type="text/javascript">
/**
Hotel Component
Handles the rendering of a single hotel
@prop hotel - hotel object, with at least a price, name, description and sale information
*/
Vue.component('hotel', {
props: ['hotel'],
template: `
<div class="card">
<div class="content">
<div :class="priceStyles" v-on:click="showOriginalPrice">£{{ hotelPrice }}</div>
<div class="header">{{ hotel.name }}</div>
<div class="meta">
<img :src="hotel.img" align="left">
{{ hotel.description }} </div>
</div>
</div>
`,
computed: {
/*
Returns whether or not this hotel is having a sale or not
*/
onSale: function() {
return this.hotel.sale.price != null;
},
/*
Returns either the hotel price, or it's sale price if it's on sale
*/
hotelPrice: function() {
if (this.onSale) {
return this.hotel.sale.price;
} else {
return this.hotel.price;
}
},
/*
Returns styling required to highlight sales
*/
priceStyles: function() {
if (this.onSale) {
return "right floated red";
} else {
return "right floated"
}
}
},
methods: {
/*
Shows an alert that lists the original price of a sale hotel, or informs user there is no sale
*/
showOriginalPrice: function() {
if (this.onSale) {
alert("Original Price - " + this.hotel.price + ", Sale Price - " + this.hotel.sale.price);
} else {
alert("Not on sale!");
}
}
}
});
/*
Launch the Vue app
*/
var app = new Vue({
el: '#vue_demo',
data: {
hotels: [],
filterSale: false
},
/*
On creation hit my test API to retrieve hotel information, and format this as json
*/
created() {
fetch('https://api.myjson.com/bins/8nfpp')
.then(response => response.json())
.then(json => {
this.hotels = json.hotels;
})
}
})
</script>
</html>