diff options
author | Bastien Wirtz <bastien.wirtz@gmail.com> | 2018-06-16 15:48:28 -0700 |
---|---|---|
committer | Bastien Wirtz <bastien.wirtz@gmail.com> | 2018-06-16 15:48:28 -0700 |
commit | 9ca12a40f404ba15d67068087dc521be7d69eefb (patch) | |
tree | f0ff26d3c8e5eded005087b7b05326374b544a88 | |
parent | e3212743b9b59e32e7bc68c387399f41f30baef6 (diff) | |
download | homer-9ca12a40f404ba15d67068087dc521be7d69eefb.tar.gz homer-9ca12a40f404ba15d67068087dc521be7d69eefb.tar.zst homer-9ca12a40f404ba15d67068087dc521be7d69eefb.zip |
Use fetch API instead of xhr.
-rw-r--r-- | app.js | 45 |
1 files changed, 15 insertions, 30 deletions
@@ -5,10 +5,11 @@ var app = new Vue({ | |||
5 | filter: '' | 5 | filter: '' |
6 | }, | 6 | }, |
7 | beforeCreate () { | 7 | beforeCreate () { |
8 | var that = this; | 8 | let that = this; |
9 | |||
9 | return getConfig().then(function (config) { | 10 | return getConfig().then(function (config) { |
10 | // Splice services list into groups of 3 for flex column display | 11 | console.log(config); |
11 | var size = 3; | 12 | const size = 3; |
12 | config.services.forEach(function(service) { | 13 | config.services.forEach(function(service) { |
13 | service.rows = []; | 14 | service.rows = []; |
14 | items = service.items; | 15 | items = service.items; |
@@ -17,42 +18,26 @@ var app = new Vue({ | |||
17 | } | 18 | } |
18 | 19 | ||
19 | if (service.rows.length) { | 20 | if (service.rows.length) { |
20 | var last = service.rows.length-1; | 21 | let last = service.rows.length-1; |
21 | service.rows[last] = service.rows[last].concat(Array(size - service.rows[last].length)); | 22 | service.rows[last] = service.rows[last].concat(Array(size - service.rows[last].length)); |
22 | } | 23 | } |
23 | }); | 24 | }); |
24 | that.config = config; | 25 | that.config = config; |
26 | }).catch(function () { | ||
27 | console.error('Fail to get config'); | ||
25 | }); | 28 | }); |
26 | } | 29 | } |
27 | }); | 30 | }); |
28 | 31 | ||
29 | 32 | ||
30 | function getConfig() { | 33 | function getConfig() { |
31 | return new Promise(function (resolve, reject) { | 34 | return fetch('config.yml').then(function(response) { |
32 | var xhr = new XMLHttpRequest(); | 35 | if (response.status !== 200) { |
33 | xhr.open('GET', 'config.yml'); | 36 | return; |
34 | xhr.onload = function () { | 37 | } |
35 | if (this.status >= 200 && this.status < 300) { | 38 | |
36 | try { | 39 | return response.text().then(function(body){ |
37 | var data = jsyaml.load(xhr.response); | 40 | return jsyaml.load(body); |
38 | resolve(data); | 41 | }); |
39 | } catch (e) { | ||
40 | console.error('fail to parse config file'); | ||
41 | reject(); | ||
42 | } | ||
43 | } else { | ||
44 | reject({ | ||
45 | status: this.status, | ||
46 | statusText: xhr.statusText | ||
47 | }); | ||
48 | } | ||
49 | }; | ||
50 | xhr.onerror = function () { | ||
51 | reject({ | ||
52 | status: this.status, | ||
53 | statusText: xhr.statusText | ||
54 | }); | ||
55 | }; | ||
56 | xhr.send(); | ||
57 | }); | 42 | }); |
58 | } \ No newline at end of file | 43 | } \ No newline at end of file |