[Seaside] option/selected and s-exprs?

Avi Bryant seaside@lists.squeakfoundation.org
Tue, 30 Jul 2002 11:00:42 -0700 (PDT)


On Tue, 30 Jul 2002, Ragnar Hojland Espinosa wrote:

> I want to do something very simple.  I want to display a listbox of
> companies, with the company with the id that matches with instance variable
> companyID selected.

First, a note about this - I gather from your other posts that you're
interacting a lot with SQL databases, and that companyID is a primary key.
Seaside goes a lot smoother when you work with references to objects
instead of primary keys - I would recommend looking into an OR mapping
like Glorp, or rolling a simple on yourself.  But that's a side issue.

> addHandlers
> (template elementName: 'ExMyComponent')
> 	  onDisplay: [:d | self populate)

I don't really understand what this is doing here - the point of
onDisplay: is to synchronize the parent's state with the child, but
clearly that's not happening here (why are you calling "self populate" on
the parent every time a particular child is displayed?).  If you want
something to happen every time the page is shown, put it in #aboutToView.

> populate
> "fills the member variable companyID with the magically appropiate value"
>
> companies
> "returns an OrderedList of Dictionaries with keys (companyid, companyname)"
>
> html
> "fragment of.."
> (select (name '@c/companies'
> 	(option name: 'companyID' value: '[c.companyid]' '[c.companyname]') ))

is "name" supposed to be an attribute of the select tag?  In which case it
should be

(select name: '@c/companies'
   ...

And you haven't made your option tag dynamic.  You could try this:

(select name: '@c/companies'
  (option name: '@companyID'
    '[c.companyName]'))

Except that by default it'll be trying to match the company ("c") with
companyID, since that's what you're iterating over (option tags look at
the top of the locals stack by default).  This would be an example of
things going smoother when you use real object references...

Since you're using primary keys, it's a bit messier.  Setting the value
attribute to 'c.companyID' was the right idea, but as you point out,
the [] syntax doesn't work inside attributes.  So you need to do it in
addHandlers instead:

addHandlers
  (template elementNamed: 'companyID')
    onDisplay:
       [:option |
       option attributeAt: 'value' put: ((locals at: #c) companyID).
       option defaultDisplayEvent]

I think that should work for you.

Cheers,
Avi