Jaxer.async

Jaxer.async is the namespace which holds callbacks to be called to the server. We will use the following code to demonstrate this.

<!doctype html>
<html>

	<head>
		
		<title>Jaxer Async</title>
		<meta charset='utf-8'/>

		<script runat="server">

			function getDBConnection() {
				...
			}

		</script>

		<script runat="server-proxy">

			function getNotes() {
				...
			}

			function saveNote(text) {
				...
				return id;
			}

			function updateNote(id, text) {
				...
				return true;
			}

		</script>


	</head>

	<body>

		...

		<script src="my_script.js"></script>

	</body>

</html>

The example is a heavily truncated note taking application. There are three scripts located on this page. The first at the top are the server scripts that run exclusively on the server, and must be called from a server-proxy function. Next is the server proxy. These are the functions which the client will be able to call. So we have the three callbacks of:

  1. Jaxer.async.getNotes
  2. Jaxer.async.saveNote
  3. Jaxer.async.updateNote

Will be avaialble on the client. The other aspect to touch on is that Jaxer will handle the function types and automatically pass the arguments you call to the function from the client to the server. So there isn't a lot to handle. And an example implement of our client could look something like the following.

async function NoteDemo() {

	var text = "Hello, World!";
	var id = await Jaxer.async.saveNote(text);
	text += " From Jaxer";
	var updated = await Jaxer.async.updateNote(id, text);

	if(!updated) {
		console.error("something went wrong when saving");
		return;
	}

	console.log("Note updated on the server");		

}