Using the MooTools Ajax Client with SMODL
Overview
MooTools is a modular JavaScript framework which aims to enhance the developer experience by providing features like object orientation, events and extensions of native elements. The Class object gives you a simplified approach to inheritance and lets you inherit or extend existing functionality with ease. The framework has a big focus on code-reusability.
This
tutorial shows how to generate an AjAX proxy object from a SMODL
model, and how to use it with MooTools to communicate with an online
SMODL service.
Prerequisites
You need the following software installed to follow this tutorial:
Java Development Kit (JDK), available from Oracle.
An Eclipse IDE for Java. The package and installation instructions can be found on Eclipse Web site. Screenshots and instructions in this document are from Eclipse JEE "Helios" edition.*
Smodl Development Suite V1.5 or newer. Please visit this site for installation instructions.
Tomcat 7.0 (optional)** available from Apache Tomcat Web site. This tutorial was written using version 7.0.14.
The online SMODL example-service is found at http://examples.smodl.org/ajax-tutorial/.
This document was written using MooTools version 1.3 and we use the Google Libraries API
to load the MooTools library.
*If you are not going to
use a
servlet-container, you can use the classic Eclipse IDE or any other
version supporting Java. Just remember that instructions and
screenshots are from the JEE package.
** Tomcat is
optional since we show how to serve your MooTools client from the
standalone SMODL server. If you rather want to use a proper
servler-container, see last section and make sure to use the Eclipse
for JEE Developers.
The standalone server is not intended to replace a full-featured
servlet-container or application-server but is convenient for efficient
and quick testing.
Step-by-step instructions
Here we show how to build a MooTools AJAX client that communicates with the SMODL "Repository Service". The "Repository Service" represents a simple account management system built in the previous tutorial series.
Create project
The SMODL file can also be fetched from the online service by appending "?smodl" to the URL: http://examples.smodl.org/ajax-tutorial/service/JSON?smodl
The generated code is a so-called proxy object built on top of the MooTools Request object which again is a wrapper around XMLHttpRequest. The proxy allows you to invoke methods declared in the SMODL file, hiding all the networking. It triggers events when the invoked methods completes, both for successful and unsuccessful JSON responses.
We now create a HTML file which uses the generated proxy and implements a simple client to the online service. The proxy object is initiated with the service URL and options (optional).
The interesting part of this code is at the end of the
script-tag where we register event. The event "domready" is called when
the DOM has successfully
been loaded. When this event is triggered, it proceeds to register
events so that our client is notified when responses to method-calls
are received. See next section for details...
Calling methods
The generated code is a proxy object which abstracts away unnecessary implementation details. This object contains the JSON-RPC calls that is generated from the SMODL file and implements events for each of the methods. It provides events for successful JSON responses as well as unsuccessful, more about this in next section.
Calling a method on the proxy object is very simple. Just use the method name and parameters as specified in the SMODL file.
service.loginUser("user", "pass");
In order for this to make sense in an AJAX context, we need to register an event and specify what happens when the function returns:
Registering events
MooTools is event based which means that we register and listen to events instead of specifying callbacks for each method-call. The generated proxy uses a simple naming convention where the name of the method defines the name of the event.
Example:
service.addEvent('getCurrentUsername', function(res) {
alert(res.result);
});
In this example we register an event called getCurrentUsername, and specify what function that should be executed upon a fired event. When successful, this function alerts the result to the user.
More about MooTools and events can be found here.
Handling errors
There are some predefined events in MooTools such as failure and timeout, which signal errors. However, these deal with the transfer of a request, not the request itself, i.e. they are fired only if the transfer itself failed. If an error occurs on the server side (e.g. the method in the service failed) the transfer is considered successful and a ''success'' event if fired. In this case ''failure'' is not fired but request contains a fault code and message. In order to handle such errors differently you can register an error handler for an error associated with a method. As with the regular events we use a simple naming convention for failure-events: <methodname>Failure.
service.addEvent('loginUserFailure', function(res) {
alert('Error ' + res.error.faultCode + " " + res.error.faultString);
});
In this example we alert the user with the fault code and message when a loginUser call fails.
The following is an example of events generated for this tutorial's SMODL file:
| Success event | Failure event |
| loginUser | loginUserFailure |
| logoffUser | logoffUserFailure |
| getCurrentUsername | getCurrentUsernameFailure |
Cross Origin Resource Sharing and MooTools
Browsers today enforce the so-called Same Origin Policy which prevents scripts from accessing resources loaded from a different domain. This makes sense in general for security-reasons, but in our case it means that our HTML file would have to be served by the same server running the service. Cross-Origin Resource Sharing (CORS) is a W3C working draft which aims to enable client-side cross-origin requests to deal safely with situations like ours.
Smodl Development Suite supports the server-part of CORS but the current version of MooTools do not support the client-part of CORS according to the standard. Hence Smodl Development Suite applies a small workaround to deal with this.
Of the major browser vendors, Opera is (at the time of writing)
still not supporting CORS. As of version 8, Internet Explorer partially
supports CORS through the XDomainRequest object but unfortunately this
isn't (at the time of writing) supported by MooTools. More details
about SMODL and CORS can be found here
- please refer to MooTools documentation for updates on browsers and
features supported by MooTool. However, the simplest way to test this
is to use Firefox or Chrome.
In order to test our MooTools-client we must load the file "index.html" into our browser. The natural way would be to load it as a local file, but this does not work in most browsers due to restrictions concerning CORS (the exception is recent versions of Firefox - see details in table linked above). Hence, we decide to serve it from a http-server...
Using the SMODL standalone server
The by far quickest and simplest way to serve your html-file is to use the in-built standalone server in Smodl Development Suite .
Note that the in-built server by default allows access only from
localhost. In order to let other computers load your client, add "-a"
to the "Program Arguments" in the "Arguments"-tab.
Using Tomcat to serve your client
If you plan to create lots of html-files and experiment with
other services, you can set up Tomcat and have it serve your
web-application. (This option requires an Eclipse-installation with JEE support.)
In the Java EE perspective, select the "Server" tab and create a new server.
Choose Apache Tomcat
When pressing "Next", you need to specify where your Tomcat
installation is located.
Add correct project
Start the Tomcat server