I am new to web dev and am trying to get data that has been inserted into a Google App Engine datastore and selectively post it onto a page using AngularJS. I am able to get Angular to work with a JSON file and can post the content of my app engine database using inline javascript on an html file, but I would like to do so using AngularJS. I tried to follow the tips listed on this page, but it seems to be missing information and doesn't explain where I should add "gapi.client.guestbook.messages. insert(message).execute();". I used objectify to annotate the api and java to write the new entity and api.
For example, the following code which references a json file in my program, works fine.
listingControllers.controller('ViewItemsCtrl', [
'$scope',
'$routeParams',
'$http',
function($scope, $routeParams, $http) {
$http.get('phones/' + $routeParams.phoneId + '.json').success(
function(data) {
$scope.phone = data;
$scope.mainImageUrl = data.images[0];
});
$scope.setImage = function(imageUrl) {
$scope.mainImageUrl = imageUrl;
}
$('.dropdown-toggle').dropdown();
} ]);
But when I try to make a controller to reference the Google datastore, I don't know exactly what to do. My best guess based on what I have researched is the following, but it's clearly not connecting to the database. If I do a simple get or insert call to the app engine without Angular, I first call function init() which loads gapi and calls getListings and insertListings. Do I need to do this in the controller? Here is my code which is partially based on this.
listingControllers.controller('datastoreTestCtrl', [
'$scope',
'$routeParams',
'$http',
function datastoreTestCtrl($scope) {
$scope.insertListing= function() {
message = {
"title" : $scope.title,
"price" : $scope.price
};
gapi.client.listingserviceapi.insertListing(message).execute()
}
$scope.list = function() {
gapi.client.listingserviceapi.getListings().execute(function(resp) {
$scope.messages = resp.items;
$scope.$apply();
});
}
}
]);
The following code is for the inline javascript that I can use to get and insert data from the datastore. I successfully used it to upload new data, but I can't seem to figure out how to use the inserted data on new pages. Thank you and please let me know if you know how to do this or can elaborate on the intro on the google web apps page.
<script type="text/javascript">
function init() {
//Parameters are APIName,APIVersion,CallBack function,API Root
gapi.client.load('listingserviceapi', 'v1', null, 'https://molt-team-233.appspot.com/_ah/api');
document.getElementById('getListings').onclick = function() {
getListings();
}
document.getElementById('insertListing').onclick = function() {
insertListing();
}
}
//List Quotes function that will execute the list call
function getListings() {
gapi.client.listingserviceapi.getListings().execute(function(resp) {
if (!resp.code) {
resp.items = resp.items || [];
var result = "";
for (var i=0;i<resp.items.length;i++) {
result = result+ resp.items[i].title + "..." + "<b>" + resp.items[i].description + "</b>" + "[" + resp.items[i].price + "]" + "<br/>";
}
document.getElementById('getListingsResult').innerHTML = result;
}
});
}
//Insert function
function insertListing() {
//Validate the entries
var _title = document.getElementById('title').value;
var _price = document.getElementById('price').value;
//Build the Request Object
var requestData = {};
requestData.title = _title;
requestData.price = _price;
gapi.client.listingserviceapi.insertListing(requestData).execute(function(resp) {
if (!resp.code) {
//Just logging to console now, you can do your check here/display message
console.log(resp.id + ":" + resp.author + ":" + resp.message);
}
});
}
</script>
<script src="https://apis.google.com/js/client.js?onload=init"></script>