no more omit
This commit is contained in:
107
src/client/js.js
Normal file
107
src/client/js.js
Normal file
@@ -0,0 +1,107 @@
|
||||
class DB {
|
||||
constructor() {
|
||||
if (typeof(localStorage) === "undefined") {
|
||||
throw new Error('Hmmm... no "localStorage" support');
|
||||
}
|
||||
}
|
||||
|
||||
get(key) {
|
||||
b = localStorage.getItem(key);
|
||||
if (b) {
|
||||
b = JSON.parse(b);
|
||||
} else {
|
||||
b = null;
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
b = JSON.stringify(value);
|
||||
localStorage.setItem(key, b);
|
||||
}
|
||||
|
||||
del(key) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
class Requests {
|
||||
put(remote, body, callback) {
|
||||
this.http("put", remote, callback, body);
|
||||
}
|
||||
|
||||
post(remote, body, callback) {
|
||||
this.http("post", remote, callback, body);
|
||||
}
|
||||
|
||||
get(remote, callback) {
|
||||
this.http("get", remote, callback, null);
|
||||
}
|
||||
|
||||
http(method, remote, callback, body) {
|
||||
var xmlhttp = new XMLHttpRequest();
|
||||
xmlhttp.onreadystatechange = function() {
|
||||
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
|
||||
callback(xmlhttp.responseText, xmlhttp.status);
|
||||
}
|
||||
};
|
||||
xmlhttp.open(method, remote, true);
|
||||
if (typeof body == "undefined") {
|
||||
body = null;
|
||||
}
|
||||
xmlhttp.send(body);
|
||||
}
|
||||
}
|
||||
|
||||
class Games {
|
||||
constructor() {
|
||||
this.db = new DB();
|
||||
this.requests = new Requests();
|
||||
}
|
||||
|
||||
list(cb) {
|
||||
this.requests.get("/api/games", (text) => {
|
||||
var list = JSON.parse(text);
|
||||
cb(list);
|
||||
});
|
||||
}
|
||||
|
||||
get(id, cb) {
|
||||
this.requests.get("/api/games/"+id, (text, status) => {
|
||||
if (status != 200) {
|
||||
throw new Error("bad status getting game: "+status+": "+text);
|
||||
}
|
||||
var game = JSON.parse(text);
|
||||
cb(game);
|
||||
});
|
||||
}
|
||||
|
||||
create(id, cb) {
|
||||
this.requests.post("/api/games/"+id, null, (text, status) => {
|
||||
if (status != 200) {
|
||||
throw new Error("bad status creating game: "+status+": "+text);
|
||||
}
|
||||
var game = JSON.parse(text);
|
||||
cb(game);
|
||||
});
|
||||
}
|
||||
|
||||
update(id, game, cb) {
|
||||
this.requests.put("/api/games/"+id, JSON.stringify(game), (text, status) => {
|
||||
if (status != 200) {
|
||||
throw new Error("bad status updating game: "+status+": "+text);
|
||||
}
|
||||
var game2 = JSON.parse(text);
|
||||
cb(game2);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
games = new Games()
|
||||
games.create("id", console.log);
|
||||
games.list((ids) => {
|
||||
console.log(ids);
|
||||
ids.forEach((id) => {
|
||||
games.get(id, console.log);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user