-
-
Save brammond-zz/21915440f2e8ddb497ba to your computer and use it in GitHub Desktop.
Gets the stuff our of our database for our first attempt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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