Skip to content

Instantly share code, notes, and snippets.

@brammond-zz
Last active August 29, 2015 14:01
Show Gist options
  • Save brammond-zz/21915440f2e8ddb497ba to your computer and use it in GitHub Desktop.
Save brammond-zz/21915440f2e8ddb497ba to your computer and use it in GitHub Desktop.
Gets the stuff our of our database for our first attempt
function getScores() {
$.ajax({
url: DATABASE + "/_design/scores/_view/playername_w_score",
success: function (data) {
var view = JSON.parse(data);
var scores = [];
$(".alert-success").show();
$(view.rows).each(function (index, item) {
scores.push(item.value);
});
displayScores(scores);
},
error: function () { $(".alert-danger").show(); }
});
}
function displayScores(scores) {
var html = "<table class=\"table .table-hover>\"";
$(scores).each(function (index, score) {
var edit = "<input type='button' value='Edit Name' " +
"onclick='editPlayerName(" + JSON.stringify(score) + ")' />";
var del = "<input type='button' value='Delete' " +
"onclick='deleteScore(" + JSON.stringify(score) + ")' />";
html += "<tr>";
html += "<td>" + score.scorevalue + "</td>";
html += "<td>" + score.playername + "</td>"
html += "<td>" + edit + "</td>";
html += "<td>" + del + "</td>";
html += "</tr>";
});
html += "</table>";
$('#scores').empty();
$('#scores').append(html);
}
function addScore() {
var scorevalue = prompt("Enter a score");
var playername = prompt("Enter a name");
if (scorevalue && playername) {
var score = {
"scorevalue": scorevalue,
"playername" : playername
};
$.ajax({
type: "POST",
url: DATABASE,
contentType: "application/json",
data: JSON.stringify(score),
success: function () {
getScores();
}
});
}
}
function editPlayerName(score) {
var newplayername = prompt("New name", score.playername);
if (newplayername) {
score.playername = newplayername;
$.ajax({
type: "PUT",
url: DATABASE + "/" + score._id,
contentType: "application/json",
data: JSON.stringify(score),
success: function () {
getScores();
}
});
}
}
function deleteScore(score) {
var doit = confirm("Do you really want to delete this score '" +
score.scorevalue + "'?");
if (doit) {
$.ajax({
type: "DELETE",
url: DATABASE + "/" + score._id + "?rev=" + score._rev,
success: function () {
getScores();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment