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:

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.

  1. 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


  2. Generating the Proxy object

    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.

  3. Generate MT Ajax

  4. Create the HTML file

    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...

  5. How to use the Proxy object

  6. Serving the client

    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.)