bouquins-bchs/index.js

57 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-12-30 16:16:43 +00:00
(function(root) {
'use strict';
2016-12-30 17:18:11 +00:00
function sendQuery(url, error, success)
2016-12-30 16:16:43 +00:00
{
var xmh = new XMLHttpRequest();
var v;
xmh.onreadystatechange = function() {
v = xmh.responseText;
2016-12-30 17:18:11 +00:00
if (xmh.readyState === 4 && xmh.status === 200) {
var res;
try {
res = JSON.parse(v);
} catch (err) {
if (null !== error)
error(err.name, err.message);
}
2016-12-30 16:16:43 +00:00
if (null !== success)
2016-12-30 17:18:11 +00:00
success(res);
2016-12-30 16:16:43 +00:00
} else if (xmh.readyState === 4) {
if (null !== error)
error(xmh.status, v);
}
};
xmh.open('GET', url, true);
xmh.send(null);
}
function stdError(code, resp) {
2016-12-30 17:18:11 +00:00
console.log('ERROR ' + code + ': ' + resp);
2016-12-30 16:16:43 +00:00
}
function indexSuccess(resp) {
2016-12-30 17:18:11 +00:00
app.books = resp;
app.booksCount = app.books.length;
2016-12-30 16:16:43 +00:00
}
2016-12-30 17:18:11 +00:00
function loadIndex() {
sendQuery('cgi-bin/bouquins/books', stdError, indexSuccess);
2016-12-30 16:16:43 +00:00
}
root.loadIndex = loadIndex;
})(this);
var app = new Vue({
el: '#app',
data: {
2016-12-30 17:18:11 +00:00
books: [],
booksCount: 0
},
mounted: function() {
loadIndex();
2016-12-30 16:16:43 +00:00
}
})