Thursday, 13 March 2008

Snippet: how to trigger events on a checkbox in Seaside

Richard Eng was having trouble using a checkbox to trigger a change in the contents of a textarea.

Lukas responded that:
To trigger a callback of a form element, you need to specify this form element with #triggerFormElement:. As the comment of this method says, this does not work for multi-select lists and checkboxes, as those two form elements internally depend on another hidden form element. So for your checkbox you need to trigger the whole form. Give it an (unique) id and use #triggerForm: with this id.
(my italics)

Gerhard Obermann kindly provided a working sample:

renderContentOn: html
"requires 'set' to be defined as an instvar"
| formId |
formId := html nextId.
html form id: formId;
with: [
html checkbox value: false; callback: [ :v | set := v ];
onClick: (
html updater id: 'text';
triggerForm: formId;
callback: [ :r |
ifTrue: [ r text: 'My address' ]
ifFalse: [ r text: '' ]]).

html textArea id: 'text';
with: [ html text: 'Empty' ]].
This was a great help to Richard, prompting him to ask why there isn't more information like this out there - much of what he found while teaching himself Squeak and Seaside was out-of-date and wrong. Maybe a snippets library would be a good starting point?

No comments: