Resource CenterHow to Build an iPhone Web App with the eXo Platform 3 IDEUsing REST services and HTML5OvervieweXo Platform 3 provides a powerful web-based IDE that makes it easy to build a complete web application. Not only does it allow you to write client side code, like gadgets and HTML, but you can also easily deploy code in the Platform's back-end to create your own APIs. Using REST architecture and Ajax calls, developers can separate the data interface from the user interface and therefore build different interfaces depending on the device used to browse the web. The application has two components:
Preparation
Deploying the REST serviceseXo Platform 3 offers a set of REST services to access some resources and application logic, but if these APIs do not fit your needs, it is very easy to create a custom API on the fly that can be deployed in the platform. The first step is to write the REST service, which requires a working knowledge of the eXo Java APIs and of the eXo Java Content Repository. For the WCM Services, the documentation can be found here. Start by creating a "rest" folder where we will add all the server-side code. To write our service, we will need a couple utility classes that we will help us build our JSON response. Create a "utils" folder, and add the following classes: Validate these classes in the IDE using the upper right button and they become automatically available in the classpath. We can now start writing our main class for the REST Service and import these classes with: import utils.*; The next step is writing the REST Service that will expose taxonomy categories and articles. We will not go into the details of the code, but you can take a look at the file here. This service has two endpoints.
/categories/all/{repoName}/{catePath:.*}/
/categories/articles/{repoName}/{docPath:.*}/
After adding all the files to the IDE, you should have the following setup: After validating all the classes, you should be able to deploy the REST service. If the service is successfully deployed, you can test it using its URL. In the case of our sample app, these URLs are: http://localhost:8080/rest/private/categories/all/repository/acme/ or if you are using the online demo: http://platform.demo.exoplatform.org/rest/private/categories/all/repository/acme/You should see the following JSON response: Building the User InterfaceThe second step is building the UI. Let's create a "ui" folder. In order to get an iPhone look and feel, we will use the jQtouch library. Create a "jqtouch" folder and add the JavaScript libraries.We also need a "skin" folder in which to put all the CSS and images. These files can be found here: We can now write the core of the application. The mobileUI.js file will be the controller, which handles the clicks, forwards the data calls to the dataService.js and updates index.html. The dataService.js will handle the database and REST calls. Every time a request is made, the device will check if the status is online or offline using: online = navigator.onLine; If the device is online, it makes an Ajax request using:
$.getJSON("/rest/private/categories/all/repository/" + currentCategory + "/", callback);
The response to that request is then stored in the browser's local database. When the device is offline, the categories or articles are retrieved thanks to an SQL query. For example:
db.transaction(
function (transaction) {
transaction.executeSql(
'SELECT * FROM articles WHERE local_path=?;', [path],
function (transaction, result) {
callback(result);
}, errorHandler);
});
Finally, the index.html is the view. If you take a closer look, you will notice it is made mostly ofdiv tags, as the content is injected through JavaScript. We also use a small templating trick as described in this article. You can find all the files here: If you have done everything right, you should have the following file structure: The only remaining step is to make your application available offline. You can do so by adding a manifest file to cache the resources; that file can be found here. You can now turn on your iPhone (or your Safari browser) and test the application with the following URL: http://localhost:8080/rest/private/jcr/repository/dev-monit/UI/index.html Since you are accessing private services of the platform, you will be asked to login, so simply enter "john" as the username and "gtn" as the password.
|
Subscribe to our newsletter and keep up with the latest eXo news and events.