Ajax? No problem. 

If the page is only redisplayed due to error then try this:

        void onPrepareForRender() {

                // If fresh start, populate screen with initial values

                if (form.isValid()) {
                        // Do init here...
                }
        }

However, if the page is being redisplayed even when there are no errors, add a 
hidden field, say "inited", to the form:

        private boolean inited;
        void onPrepareForRender() {

                // If not initialised, populate screen with initial values

                if (!inited) {
                        // Do init here…
                        // Then at the end…
                        inited = true;
                }
        }

"Inited" won't reset to false until you do a page render request or explicitly 
set it to false.

I've successfully used this approach in the AjaxFormLoop examples, e.g.

        http://jumpstart.doublenegative.com.au/jumpstart/examples/ajax/formloop1

Cheers,

Geoff

On 05/05/2012, at 1:31 AM, Norman Franke wrote:

> On May 4, 2012, at 8:53 AM, Arno Haase wrote:
> 
>>>> onPrepare() is called before data binding, but it is called
>>>> again after data binding. So simple initialization code in
>>>> onPrepare() would overwrite the form data with the default.
>>> 
>>> This isn't correct. It's called once per request. And Tapestry
>>> uses redirect-after-post, so the form submission and the form
>>> rendering are never in the same request (unless you're doing the
>>> submission though AJAX).
>> 
>> Thanks for pointing this out.
>> 
>> But since I am using AJAX - does anyone have a recommendation for
>> where to put the initializations?
> 
> 
> Good question. I never came up with a good solution, either, without having 
> two events. One can do onActivate(), but if you are doing something slow, 
> like reading values from a database, I wouldn't recommend it. This is because 
> onActivate is called to construct the URL in any referencing page. As a 
> result, my start page calls onActivate for like 30 pages and is very slow on 
> startup.
> 
> @SetupRender is another potential place, but I don't think it's called for 
> AJAX, so I often just call it from my AJAX listener. Seems we really need an 
> @OnInitGoesHere or something. :)
> 
> Norman Franke
> Answering Service for Directors, Inc.
> www.myasd.com
> 
> 
> 

Reply via email to