Skip to content

Instantly share code, notes, and snippets.

@greenido
Created July 5, 2012 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenido/3052747 to your computer and use it in GitHub Desktop.
Save greenido/3052747 to your computer and use it in GitHub Desktop.
CRUD operations on our birra app
// Delete beer
$("#beerDelBut").click(function() {
$('#results').html(beerApp.callingServerHtml);
var req = gapi.client.birra.beers.delete({
'id' : $("#gbeerId").val()
});
req.execute(function(data) {
beerApp.showList(data);
});
});
// Add new beer
$("#beerAddBut").click(function(data) {
beerApp.clearFields();
$("#beerLocation").val(beerApp.curLocation);
$("#beerScore").change();
// Using our Geo information to have a small map of the area around us
var mapImg = '<img border=0 src="http://maps.googleapis.com/maps/api/staticmap?center=' +
beerApp.curLocation + '&zoom=14&size=262x112&maptype=roadmap&markers=color:blue%7Clabel:S%7C' +
beerApp.curLocation + '&sensor=true"/>';
$("#localMap").html(mapImg);
$('#beerDetailsModal').modal('show');
});
// Actions for the modal
$("#cancelBeer").click(function(data) {
beerApp.clearFields();
$('#beerDetailsModal').modal('hide');
});
//
// Save beer - Add new or update
//
$("#saveBeer").click(function() {
console.log("Going to save the beer...");
var features = {};
// extract all the info from the form's fields
$("#beerDetailsModal input[id^='beer']").each(function() {
features[$(this).attr('name')] = $(this).val();
});
delete features['undefined'];
var latLong = features['location'].split(",");
delete features['location'];
// Get the select value as well.
features[$('#BeerNumDrinks').attr('name')] = $('#BeerNumDrinks').val();
if ($('#upImg')[0] !== null) {
var tmpImg = $('#upImg')[0]; // keep it one image per beer
var beerImg64 = getBase64Image(tmpImg);
features['image'] = {"value" : beerImg64};
}
var req;
if ( !features['beerId']) {
// In case we have an empty beerId, do not send it,
// so the server will take it as a new beer
delete features['beerId'];
features['latitude'] = beerApp.lang;
features['longitude'] = beerApp.long;
// Add new beer
req = gapi.client.birra.beers.insert( features );
}
else {
// It's an update of a beer
features['id'] = features['beerId'];
delete features['beerId'];
features['latitude'] = latLong[0];
features['longitude'] = latLong[1];
req = gapi.client.birra.beers.update( features );
}
req.execute(function(data) {
var tmpHTML;
if (data.error && data.error.code > 200) {
console.error("Err Code: " + data.error.code + " Err: " + data.error.message);
tmpHTML = data.error.message;
}
else {
tmpHTML = '<h4>Your Beer is Safe</h4>';
tmpHTML += "<img src='img/beer24.jpg'/>"
tmpHTML += 'id: ' + data.id + " Name: " + data.beerName;
}
$('#results').html("");
$('#alertContent').html(tmpHTML);
$('.alert').show();
});
$('#beerDetailsModal').modal('hide');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment