In Seaside, anchors can be used for much more than simple links to other documents. An anchor can be assigned a callback, which is a Smalltalk block (similar to closures or anonymous methods in other languages). When the user clicks on the link, the user’s browser submits a request back to Seaside, which then runs the code in the block (it evaluates the block in Smalltalk terminology).
Here is an example of a component defining a callback which increases the value of the
count variable of the component:
WAComponent subclass: #AnchorCallbackExample
count := 0.
count := count + 1
html text: count.
callback: [ self anchorClicked ];
with: 'click to increment'
renderContentOn: creates an anchor element by sending
WAHtmlCanvas>>anchor to the canvas object (
anchor method returns an instance of
WAAnchorTag which is then used to set the callback (via the method
callback:) and text for this anchor (via the message
When the user clicks on the anchor labelled "click to increment", the callback block is executed: it sends the message
anchorClicked to the component which in turn increments the count. Once this callback is processed, Seaside renders our component (which will show the new count).
Register the above application as “anchor” and view it in your browser, see Figure 69. Clicking on the link will increment the count.
|Methods on ||Description|
| ||Specify a URL to visit when this link is clicked.|
| ||Specify a block that will be invoked when this link is clicked.|
| ||This specifies the anchor text.|
Callback Processing. When Seaside receives a request, it processes all active callbacks and then asks the component to render itself. The order of this process is important. Only when it has completed processing callbacks will it move on to the rendering phase. It will become important to remember this as you build increasingly complicated applications.