This Lab will take you through creating a simple application which will demonstrate using server side functionality to call an external server and caching the response in the [whitelabel providername] cloud.
Create a new app from scratch (not from a template). Once the app is created, choose Edit my App and delete the default contents of the client/default/index.html file.
Add the following code to the index.html file and choose Save:
<p> Current Time : </p>
<div id="current_time_response"> </div>
<button id="current_time"> Refresh time </button>
<script>
$(document).ready(function(){
$('#current_time').click(readTime);
});
function readTime() {
// Read the current time from a new Date object
var time = new Date().toString();
// Display the time in-app using jQuery
$('#current_time_response').html(time);
}
// Do an initial read of the current time
readTime();
</script>
This creates an App that displays the current time, and has a button for refreshing the time. It's only working on the client, with no server-side code involved.
Add the following code to the server-side main.js file (in /cloud/main.js).
function getCurrentTime() {
var response = $fh.web({
url: 'http://www.timeanddate.com/worldclock/city.html?n=78',
method: 'GET'
});
return {
'response': response.body
};
}
This will make an external web request to http://www.timeanddate.com/worldclock/city.html?n=78 to get the current time for Dublin, Ireland (the n=78 GET parameter indicates Dublin). Go to this URL in your browser now and use your developer tools to inspect the current time text. Notice that the current time is inside a <strong> tag with an id of "ct"
modify the readTime() function in index.html so that it calls the new server side function getCurrentTime(). Notice that this server side function returns the entire page from timeanddate.com (which is over 6,000 bytes of data). We can then use jQuery to evaluate this response and extract the required time and date information.
function readTime() {
$fh.act({
act: 'getCurrentTime'
}, function(res) {
// Convert the response into a jQuery Object
var resObj = $(res.response);
// Extract the element with the id of 'ct' - this is the element that contains the time
var time = resObj.find('#ct');
// Display the time in-app using jQuery
$('#current_time_response').html(time);
});
}
Rather than returning the entire page to the client and extracting the date on the client side, we can use a regular expression on the server side to extract the date from the response and send just this information back. This has two obvious advantages:
We will now add a new server side function which return only the date and time:
function getCurrentTimeRegEx() {
var response = $fh.web({
url: 'http://www.timeanddate.com/worldclock/city.html?n=78',
method: 'GET'
});
// Construct a regular expression to extract the date and time from the response
var reg = /(<strong id=ct[^>]*>)([^<]*)/;
//Extract the required info
var dateMatch = reg.exec(response.body);
return {
'date': dateMatch[2]
};
}
We will now modify the readTime() function to use the new server side function getCurrentTimeRegEx()
function readTime() {
$fh.act({
act: 'getCurrentTimeRegEx'
}, function(res) {
// Display the time in-app using jQuery
$('#current_time_response').html(res.date);
});
}
Notice how much simpler the client side function now is. This is the sign of a well designed app - when the majority of the client side JavaScript code is updating the UI rather than performing complex data parsing.
As per the documentation for $fh.web(), it is possible to cache the response from a web call for a specified period of time. This can be done by adding a parameter called 'period' to the $fh.web(). The value for period is the number of milliseconds to cache the response for.
Modify the getCurrentTimeRegEx() function as below to add the period parameter
function getCurrentTimeRegEx() {
var response = $fh.web({
url: 'http://www.timeanddate.com/worldclock/city.html?n=78',
period: 10000,
//Cache response for 10 seconds
method: 'GET'
});
// Construct a regular expression to extract the date and time from the response
var reg = /(<strong id=ct[^>]*>)([^<]*)/;
//Extract the required info
var dateMatch = reg.exec(response.body);
return {
'date': dateMatch[2]
};
}
Notice that with the period parameter in place, the time in-app only updates once every 10 seconds, regardless of how often the "Refresh time" button is clicked.
Comment out the period parameter and verify that the current time updates each time the "Refresh time" button is clicked.
By default, a file called index.html is used as the Entry Point for the app. This can be changed as follows:
The file test.html has now become the Entry Point for the app. The preview should refresh to show the text entered in test.html - e.g. "Hello World"
This feature can be very useful during the development & testing of an app as it allows small feature-specific test User Interfaces to be quickly developed.