Ajax

Waffle was built to be easy to develop with and this ease extends to Ajax support as well. Utilizing Ajax in Waffle application is straightforward and simple. The examples here use the well known Prototype JavaScript Framework.

Ajax Controller

We will create a simple Controller with an event (int random();) that will return a random number between 0 and 10. To make it more interesting whenever the value equals 5 the event will throw an Exception.

Ajax-ified View

So we create a simple View that will periodically (every 2 seconds is the default) request a new value from the Server.

  • We import the prototype javascript file to provide the ajax foundation
  • The custom ajax call is new Ajax.PeriodicalUpdater("random", "foobar.waffle?method=random");
  • The first argument "random" refers to the element id tag in the html (the div)
  • The second argument foobar.waffle?method=random represents what will be periodically called to the server.
    • foobar.waffle the Action is registered under the key value "foobar".
    • method=random signifies that the "random" ActionMethod method will be called on the Controller.

Wire it up with the Registrar

The FoobarController is registered under the name "foobar". Notice that the Controller is being registered to the Application level context.

The document that comes back over the wire per Ajax request, is a one-line toString() transformation on the int result of the random() method. This is performed by org.codehaus.waffle.action.ActionMethodResponseHandler.