Skip to content

Commit 8ffe70d

Browse files
committed
use Vue to fetch JSON
1 parent 089979a commit 8ffe70d

File tree

2 files changed

+36
-52
lines changed

2 files changed

+36
-52
lines changed

src/index.html

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<link rel="stylesheet" type="text/css" href="styles.css"/>
88
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon"/>
99
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
10+
<script src="https://unpkg.com/vue@2.4.2/dist/vue.min.js"></script>
1011
<title>Public APIs</title>
1112
</head>
1213
<header>
@@ -38,7 +39,7 @@ <h3 class="center-text">
3839
</tr>
3940
</thead>
4041
<tbody>
41-
<tr v-for="item in data" v-show="filtered(item)">
42+
<tr v-for="item in items">
4243
<td><a :href="item.Link">{{ item.API }}</a></td>
4344
<td>{{ item.Description }}</td>
4445
<td>{{ (item.Auth) ? item.Auth : '-' }}</td>
@@ -51,8 +52,5 @@ <h3 class="center-text">
5152
</div>
5253
<script src="scripts.js"></script>
5354
</body>
54-
<footer>
55-
<script src="https://unpkg.com/vue@2.4.2/dist/vue.min.js"></script>
56-
</footer>
5755
</html>
5856

src/scripts.js

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,41 @@
1-
loadJSON(function(response) {
2-
var items = JSON.parse(response);
3-
new Vue({
4-
data: {
5-
filter: ''
6-
},
7-
computed: {
8-
data() {
9-
return items.entries;
10-
}
11-
},
12-
methods: {
13-
filtered(item) {
14-
let show = true;
1+
new Vue({
2+
data: {
3+
filter: '',
4+
items: ''
5+
},
6+
created() {
7+
fetch('https://raw.githubusercontent.com/toddmotto/public-apis/master/json/entries.min.json')
8+
.then(data => data.json())
9+
.then(data => {
10+
this.items = data.entries;
11+
})
12+
},
13+
methods: {
14+
filtered(item) {
15+
let show = true;
16+
17+
if(this.filter.length) {
1518

16-
if(this.filter.length) {
17-
18-
show = false;
19-
20-
let filterKeyword = this.filter.toLowerCase();
21-
22-
Object.keys(item).map(function(key) {
23-
if(typeof item[key] === 'string') {
24-
let value = item[key].toString().toLowerCase();
25-
26-
if(value.includes(filterKeyword)) {
27-
show = true;
28-
$( "th" ).addClass( "lol" );
29-
}
30-
}
19+
show = false;
20+
21+
let filterKeyword = this.filter.toLowerCase();
22+
23+
Object.keys(item).map(function(key) {
24+
if(typeof item[key] === 'string') {
25+
let value = item[key].toString().toLowerCase();
3126

32-
});
33-
}
34-
return show;
27+
if(value.includes(filterKeyword)) {
28+
show = true;
29+
$( "th" ).addClass( "lol" );
30+
}
31+
}
32+
33+
});
3534
}
35+
return show;
3636
}
37-
}).$mount('#app');
38-
});
39-
40-
function loadJSON(callback) {
41-
var xobj = new XMLHttpRequest();
42-
xobj.overrideMimeType("application/json");
43-
xobj.open('GET', 'https://raw.githubusercontent.com/toddmotto/public-apis/master/json/entries.min.json', true);
44-
xobj.onreadystatechange = function () {
45-
if (xobj.readyState == 4 && xobj.status == "200") {
46-
// Required use of an anonymous callback as .open will NOT return a value
47-
// but simply returns undefined in asynchronous mode
48-
callback(xobj.responseText);
49-
}
50-
};
51-
xobj.send(null);
52-
}
37+
}
38+
}).$mount('#app');
5339

5440
function filterRows() {
5541
var input, filter, table, tr, td, i;

0 commit comments

Comments
 (0)