From 8ffe70daf1b3255063c0a91983b5002d81b447dd Mon Sep 17 00:00:00 2001 From: davemachado Date: Thu, 17 Aug 2017 11:57:55 -0400 Subject: [PATCH] use Vue to fetch JSON --- src/index.html | 6 ++-- src/scripts.js | 88 +++++++++++++++++++++----------------------------- 2 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/index.html b/src/index.html index 7eb4d54..00f6a11 100644 --- a/src/index.html +++ b/src/index.html @@ -7,6 +7,7 @@ + Public APIs
@@ -38,7 +39,7 @@ - + {{ item.API }} {{ item.Description }} {{ (item.Auth) ? item.Auth : '-' }} @@ -51,8 +52,5 @@ - diff --git a/src/scripts.js b/src/scripts.js index 91d71bc..d8198d9 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -1,55 +1,41 @@ - loadJSON(function(response) { - var items = JSON.parse(response); - new Vue({ - data: { - filter: '' - }, - computed: { - data() { - return items.entries; - } - }, - methods: { - filtered(item) { - let show = true; - - if(this.filter.length) { - - show = false; - - let filterKeyword = this.filter.toLowerCase(); - - Object.keys(item).map(function(key) { - if(typeof item[key] === 'string') { - let value = item[key].toString().toLowerCase(); - - if(value.includes(filterKeyword)) { - show = true; - $( "th" ).addClass( "lol" ); - } - } - - }); - } - return show; - } - } - }).$mount('#app'); -}); +new Vue({ + data: { + filter: '', + items: '' + }, + created() { + fetch('https://raw.githubusercontent.com/toddmotto/public-apis/master/json/entries.min.json') + .then(data => data.json()) + .then(data => { + this.items = data.entries; + }) + }, + methods: { + filtered(item) { + let show = true; -function loadJSON(callback) { - var xobj = new XMLHttpRequest(); - xobj.overrideMimeType("application/json"); - xobj.open('GET', 'https://raw.githubusercontent.com/toddmotto/public-apis/master/json/entries.min.json', true); - xobj.onreadystatechange = function () { - if (xobj.readyState == 4 && xobj.status == "200") { - // Required use of an anonymous callback as .open will NOT return a value - // but simply returns undefined in asynchronous mode - callback(xobj.responseText); - } - }; - xobj.send(null); - } + if(this.filter.length) { + + show = false; + + let filterKeyword = this.filter.toLowerCase(); + + Object.keys(item).map(function(key) { + if(typeof item[key] === 'string') { + let value = item[key].toString().toLowerCase(); + + if(value.includes(filterKeyword)) { + show = true; + $( "th" ).addClass( "lol" ); + } + } + + }); + } + return show; + } + } +}).$mount('#app'); function filterRows() { var input, filter, table, tr, td, i;