Erste Praxiserfahrungen mit Backend as a Service

Seite 7: Beispiel mit apiOmat

Inhaltsverzeichnis
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<script charset="utf-8" src="com/apiomat/apiomat.js"></script>
<title>apiOmat test</title>
</head>

<body>
<script type="text/javascript">
function init() {
/* Create a new member/user of your app */
var user = new Apiomat.User();
user.setUserName("user1");
user.setPassword("geheim123");
Apiomat.Datastore.configure(user);

var saveCB = {
onOk: function () {
//Successfully signed up. let's go
},
onError: function (error) {
//DO error handling on signup failure
}
};

user.loadMe({
onOk: function () {
//login was successfull. let's go
},
onError: function (error) {
//auth error occured so let's create a sign up
if (error.statusCode === Apiomat.Status.UNAUTHORIZED) {
user.save(saveCB);
} else {
//handle other errors
}
}
});
}

function store() {
//Now you can create objects of your class with this new user..
var myPerson = new Apiomat.Person();
//you may set attributes here...
myPerson.setVorname('Max');
myPerson.setNachname('Mustermann');
myPerson.setAlter(35);

myPerson.save({
onOk: function () {
//object successfully saved
console.log("Saved succesfully Person");
},
onError: function (error) {
console.log("Some error occured. " + error.statusCode + " --> " +
error.message);
}
});
}

function fetch() {
Apiomat.Person.getPersons(undefined, {
onOk: function (persons) {
for (var i = 0; i < persons.length; i++) {
console.log(persons[i]);
}
},
onError: function (error) {
console.log("Some error occured. " + error.statusCode + " --> " +
error.message);
}
});
}

function query(vorname) {
Apiomat.Person.getPersons('vorname==' + vorname, {
onOk: function (persons) {
for (var i = 0; i < persons.length; i++) {
console.log(persons[i]);
}
},
onError: function (error) {
console.log("Some error occured. " + error.statusCode + " --> " +
error.message);
}
});
}
</script>
</body>

</html> (ane)