Ah, the once only pattern.
Here's an example from DatePicker:
public boolean isFirstTime()
{
return _firstTime;
}
protected void prepareForRender(IRequestCycle cycle) throws
RequestCycleException
{
_firstTime = (cycle.getAttribute(FIRST_USE_ATTRIBUTE_KEY) == null);
if (_firstTime)
cycle.setAttribute(FIRST_USE_ATTRIBUTE_KEY, Boolean.TRUE);
super.prepareForRender(cycle);
}
You pass the firstTime property into the script as an input symbol, and put
a conditional around the variable declaration.
----- Original Message -----
From: "Craig Goss" <[EMAIL PROTECTED]>
To: "Tapestry-Developer" <[EMAIL PROTECTED]>
Sent: Wednesday, September 11, 2002 11:33 PM
Subject: RE: [Tapestry-developer] script questions
> Howard Ship wrote:
>
> > There's no reason why you couldn't have,
> > in your script, ...
> >
> > <body>
> >
> > ...
> >
> > var myVariable = ....;
> >
> > function ${some.symbol)()
> > {
> > myVariable = ....;
> > }
>
> That'd be true as long as there is only a single component associated with
> the script. In this case I want to include multiple components on a page,
> each of which can set the value of a single global variable. For example,
> in the case of a MenuItem I have a page containing the following:
>
> <component id="menu1" type="MenuItem">
> <binding name="default" expression="assets.menu1_default"/>
> <binding name="select" expression="assets.menu1_selected"/>
> </component>
> <component id="menu2" type="MenuItem">
> <binding name="default" expression="assets.menu2_default"/>
> <binding name="select" expression="assets.menu2_selected"/>
> </component>
>
> Each menu item knows how to manage its images and, of the group, there's a
> single 'selected' item whose value is stored in the global. If I declare
> the global inside MenuItem.script, it gets duplicated for each component
> when the page is generated. If, instead, I pop it into a include-script,
it
> only shows up once and therefore acts as a global variable that can be set
> by any of the components. I suppose I could also drop it into the page
> script that wraps the components but that doesn't seem to be a very good
> solution since the variable would then be separated from the components
that
> care about it. It also assumes that I have a page script which, in this
> case, I don't.
>
> Ideally, I'd like to have a place in the script where I could declare vars
> that only show up once regardless of the number of components wrapped on a
> page. Perhaps something like this:
>
> <global>
> <!-- only appears once -->
> var myVariable;
> </global>
> <body>
> function ${onMouseClick}()
> <!-- do something with remembered object -->
> if (myVariable != undefined) {
> do something ...;
> }
> <!-- remember object for next time -->
> myVariable = ${somesymbol}
> }
> </body>
>
> So, when the page is generated you get something like this:
>
> function click_menuitem_0()
> {
> if (myVariable != undefined) {
> do something ...;
> }
> myVariable = document.menuitem_0;
> }
> function click_menuitem_1()
> {
> if (myVariable != undefined) {
> do something ...;
> }
> myVariable = document.menuitem_1;
> }
>
> Hope this makes some sense. I'm still getting to know Tapestry, so if I'm
> missing something or there are other ways to approach the problem let me
> know.
>
>
>
>
> -------------------------------------------------------
> In remembrance
> www.osdn.com/911/
> _______________________________________________
> Tapestry-developer mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/tapestry-developer
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
Tapestry-developer mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/tapestry-developer