[Seaside] option/selected and s-exprs?
Julian Fitzell
seaside@lists.squeakfoundation.org
Tue, 30 Jul 2002 11:41:08 -0700
Ragnar Hojland Espinosa wrote:
> On Tue, Jul 30, 2002 at 11:00:42AM -0700, Avi Bryant wrote:
>
>>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.
>
>
> Thanks, I'll look into that. I'm trying to get a decent app together in a
> language I dont know, under an app server I don't know, and probabily without
> knowing about tools i should be using either. And sort of time pressed. A nice
> reciepe ;)
>
>
>>>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.
>
>
> Aha. Okay thanks. Yes, the form in this case was tagged as ExMyComponent
> to fillit up. Out of curiosity, is there a particular problem with my workaround?
>
>
>>>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'
>
>
> Good catch.. that was a typo in my post.
>
>
>>And you haven't made your option tag dynamic. You could try this:
>>
>>(select name: '@c/companies'
>> (option name: '@companyID'
>> '[c.companyName]'))
>
>
> Yeah, same as above.
>
>
>>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...
>
>
> Figured just up to there.. then it was time to pray to the seaside gods for
> assistance ;)
>
>
>>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.
>
>
> Thtat was it. Since c is a Dictionary, it ended up looking as:
>
> option attributeAt: 'value' put: ((locals at: #c) at: 'companyid').
>
> Nowwww.. could you explain what does (locals at: #c) and why do you need to
> do the option defaultDisplayEvent so we clueless people don't have to play
> monkey? :)
>
"locals" refers to the stack of local variables that have been created
while processing the template. "c" in this case is the variable that
was created by "@c/companies" to hold each company as you iterate.
And you need to call defaultDisplayEvent because you are overriding the
onDisplay: handler. Calling defaultDisplayEvent is telling it to also
do what it would have done if you hadn't overridden it (like calling
"super" in an object method).
Julian
--
julian@beta4.com
Beta4 Productions (http://www.beta4.com)