Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For client side support, I suggest we keep JavaScript / Ajax functions in Velocity templates instead of wrapping/hiding it in a Java API. By hosting the JavaScript it in a template we One of the advantages of using Velocity is one can immediately see changes to the template, no compilation or redeploy step is required. This also allows Click's Ajax support to be fairly transparent and enables users to easily customize the templates for their own needs.

Next I'll cover the proposals to handle the Ajax request and response on the server-side.

1. Ajax aware Page methods

The simplest way to improve Ajax support is to allow Page methods to be called directly from the browser. The Page method could return a Partial response that contains the data to return to the browser.

In addition users can use their favorite JavaScript frameworks and plugins without having to learn the equivalent Java API.

Some frameworks prefer to hide JavaScript from developers as they feel the complexity of JavaScript will be overwhelming. Instead a Java API is exposed against which the developers can code. This is great, until it breaks, in which case the developer (remember he/she doesn't know any JavaScript) needs to find a fix. To find a fix the developer will rely heavily on the framework developers for support and in many cases will need to start learning and understanding JavaScript anyway (smile)

I acknowledge that JavaScript can be a pain, however I believe most of the problems come from different browser implementations of the specification. With the advent of modern JavaScript libraries such as jQuery, Prototype, YUI, Mootools etc, I believe most of the pain points have been addressed.

Another problem I see with hiding JavaScript through a Java API is that the framework will be in a constant battle to keep up with the feature set of the JavaScript libraries. Consider having things properly documented and you can see that keeping the Java API's up to date is a non trivial task.

So in summary, I suggest we allow users to write client-side code in JavaScript using their favorite library. Click should be concerned with handling the incoming Ajax request and passing back a partial result object.

Next I'll cover the proposals to handle the Ajax request and response on the server-side.

1. Ajax aware Page methods

The simplest way to improve Ajax support is to allow Page methods to be called directly from the browser. The Page method could return a Partial response that contains the data to return to the browser.

Code Block
titleMyPage.java
borderStylesolid
public MyPage extends Page {

  public Partial loadLabel() { extends Page {

  public Partial loadLabel() {
    // A partial object provides a way to return a partial response to the browser. Any content added to the Partial object,
    // is Astreamed partialback objectto providesthe aclient
 way to return aPartial partial response to the browser. Any content added to the Partial object,
    // is streamed back to the client
    Partial partial = new Partial();
    partial.add(new Label("hello"));
    return partial;
  }
}

The example client-side Ajax call will need slight modification, since a Page method is invoked instead of a Control:

Code Block
titlemy-page.js
borderStylesolid

  $.get("my-page.htm", { mylink: 1, event: event.type, page-method: loadLabel }, success);
 = new Partial();
    partial.add(new Label("hello"));
    return partial;
  }
}

The example client-side Ajax call will need slight modification, since a Page method is invoked instead of a Control:

Code Block
titlemy-page.js
borderStylesolid

  $.get("my-page.htm", { mylink: 1, event: event.type, page-method: loadLabel }, success);

In order to support invocation of Page methods, Click should be enhanced so that if the 'page-method' request parameter is present, that request parameter value will be the Page method to invoke. The signature of the method must be: "public Partial methodName()". Except for creating the Page object, no other page events (onInit, onRender etc) are executed.

Please note that this feature could be useful for non Ajax requests as well. Consider a dynamic <img> element that displays a different image depending on its parameters. The ability to invoke a Page method directly would be useful in this case as the Page method implementation could inspect the request parameters, grab the image from the database and stream back the image to the browser. All this without having to invoke other Page events (onInit, onProcess) or having to set the Page path to null, which always trips new usersIn order to support invocation of Page methods, Click should be enhanced so that if the 'page-method' request parameter is present, that request parameter value will be the Page method to invoke. The signature of the method must be: "public Partial methodName()". Except for creating the Page object, no other page events (onInit, onRender etc) are executed.

Pros:

  • simple to implement
  • easy for users to understand and use

...