Fan

 

WebWidget

Overview

WARNING: the webapp framework is still an early prototype, so will be going through many changes during development

Widget extends Weblet to make it easier to build reusable weblets:

  • Buffered output to allow composing Widgets
  • Route requests to methods

Buffered Output

The primary feature Widget adds to Weblet is the ability to compose multiple Widgets into the resulting HTML page. Since all widgets need to have the opportunity to insert content into the <head> tag, Widget buffers its output using the head and body fields. You should never call res.out directly from a Widget.

class MyWidget : Widget
{
  override Void onGet()
  {
    // always use head and body bufs
    head.title.w("My title!").titleEnd
    body.h1.w("This is my widget!").h1End
  }
}

The root Widget that handles the request will automatically pipe the buffers to the actual response stream. That Widget will also handle writing any markup needed to make the output a valid HTML page, like the DOCTYPE and <html> tags. The default implementation will produce a valid XHTML 1.0 Strict document. You can customize that behavoir by overriding the startRes and finishRes methods.

Route Requests to Methods

The call method returns a Uri that can be used to automatically route requests to specific methods on a Weblet. The normal Widget pipeline is still used, so the output will be buffered, and startRes and finishRes will be called.

class MyWidget : Widget
{
  override Void onGet()
  {
    // forms
    uri := call(#formCallback)
    body.form("method='post' action='$uri'")
    body.p.textField("name='foo'").pEnd
    body.p.submit.pEnd
    body.formEnd

    // ajax
    uri = call(#ajaxCallback)
    body.a(`#`, "onclick='myFavJsLib.ajax.post(\"$uri\"); return false;")
    body.w("Callback")
    body.aEnd
  }

  Void formCallback() { ... }  // called when form is submitted
  Void ajaxCallback() { ... }  // called when link is clicked
}