Dynamic Web Development with Seaside

20.4.5Wrap Up

Before using AJAX you have to make several decisions. First you must decide which AJAX strategy you want to use.

  • PTFactory>>request — The requestor does not send anything back to the client. It solely sends a request and pushes data to the server.
  • PTFactory>>updater — The updater updates a single part of the page. You need to provide the ID of the DOM element to update and a callback block that expects one parameter to render the partial XHTML on.
  • PTFactory>>periodical — The periodical updater is an updater that is periodically executed. Additionally you need to specify an update frequency.
  • PTFactory>>evaluator — The evaluator injects new JavaScript code into the web browser. Its callback block expects one parameter that will accept new JavaScript snippets.

After having chosen the AJAX strategy you might want to specify additional options and declare state that should be transmitted to the server. The most common ones are:

  • PTAjax>>triggerForm: — Serialize a complete form and trigger all its associated callbacks. Note that the callbacks of submit buttons are ignored to preserve consistency, use the callback to trigger specific code evaluation.
  • PTAjax>>triggerFormElement: — Serialize a form ”element” such as a text input field and triggers its associated callback. Note that this does not work for all form elements. For example, check-boxes depend internally on other hidden form elements. Submit-button callbacks are ignored.
  • PTAjax>>callback:value: — Serialize the result of evaluating the value as a JavaScript expression on the client and pass it as an argument into the callback block.

Furthermore you might want to register some events to get notified about the state of the AJAX action. The most common events are listed below.

  • PTAjax>>onSuccess: — Invoked when a request completes and its status code is undefined or belongs to the 200 family.
  • PTAjax>>onFailure: — Invoked when a request completes and its status code exists but is not in the 200 family.
  • PTAjax>>onComplete: — Invoked at the very end of a request’s life-cycle, once the request completed, status-specific callbacks were called, and possible automatic behaviors were processed.

The above events and some other AJAX options can also be globally set using PTResponders. This is useful to define an error handler once for the whole page in case the session expires. The following code snipped displays an error message and triggers a full refresh whenever that happens:

html document
addLoadScript: (html prototype responders
onFailure: (html javascript alert: 'Session Expired') ,
(html javascript refresh))

Copyright © 14 April 2024 Stéphane Ducasse, Lukas Renggli, C. David Shaffer, Rick Zaccone
This book is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 license.

This book is published using Seaside, Magritte and the Pier book publishing engine.