You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Arno Haase <ar...@haase-consulting.com> on 2012/05/04 13:43:27 UTC

Best place to initialize form data

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi everyone,

this may be an obvious newbie question, but I have found no answer
that looks good to me. Anyway, here goes:

What is the best place to initialize a property with a default value
for a form?

I am looking at a page that makes heavy use of AJAX to re-render the
form, displaying or hiding stuff based on some property values in the
form. I also want the page to be stateless on the server side. The
form is so specialized that using BeanEditor & Co does not fit - I am
aware that those components pretty much take care of this problem for
simple forms.

That's just for background, I have the form with its logic working.

I currently initialize the property in pageAttached() because that
gets called before the form does its data binding:


public class MyPage {
    @Property String p;

    // lots of other properties for the form, plus
    //logic for partial re-rendering

    public void pageAttached() {
        this.p = "default value";
    }
}

Now pageAttached() is deprecated, and I understand the reasons
(abolition of the page pool). But what other place fits the bill?

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.

onPrepareForSubmit() is not called when the page is first rendered.

onPrepareForRender() is called when the page is first rendered, but on
subsequent submits for partial re-rendering it is called after data
binding.

Using a getter method that checks for null more or less works but is
fragile with regard to null values being bound from the form (not sure
if that can actually happen, but it has a fragile and non-obvious feel
to it):

public String getP() {
  if (this.p == null) {
    this.p = "default value";
  }
  return this.p;
}

This also distributes the initialization to many methods, and I would
like it better to have it in a single method.

So - am I overlooking something?

All suggestions are much appreciated!

- - Arno



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+jwN8ACgkQbmZsMyUPuXTlgACgmH301OT63HeTMxC0qziLS0Uu
G2cAoO1bZOmiezPjaXN9B4lh8JBX24k6
=fC2k
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> you can use the onActivate method in the following form:
> 
> class MyPage { @Property String x;
> 
> public onActivate(EventContext context) { if (context.getCount() >
> 0) { this.x = context.get(String.class, 0); } else { this.x =
> "default value"; } } }
> 
> the "onActivate" event handler with the single EventContext
> parameter gets invoked in any case, regardless of the activation
> context parameter count (gets invoked with no activation context
> too!).

Thanks for pointing this out! Not quite as easy to read as an
'onActivate' method with an explicit parameter list, though.

Can someone point tell me the rationale for Tapestry calling all
onActivate() methods, and especially calling those with fewer
parameters *after* those with more parameters? Having them called in
reverse sequence would simplify initialization for me.

There are probably good reasons for doing it the way it is done, and I
would like to understand them ;-)

> nice to see you working with tapestry, i know your name from the
> german java scene and appreciate, that tapestry gets increasingly
> attention in germany ;)

;-)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+mEKoACgkQbmZsMyUPuXS8agCgyArTeRQn3WA6c1uFR90yqJIV
OkgAmgLo2dGzgD7kvjPpusFMC8CIFzjB
=CWN+
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Felix Gonschorek <fe...@netzgut.net>.
Hi Arno,

you can use the onActivate method in the following form:

class MyPage {
  @Property String x;

  public onActivate(EventContext context) {
    if (context.getCount() > 0) {
      this.x = context.get(String.class, 0);
    }
    else {
      this.x = "default value";
    }
  }
}

the "onActivate" event handler with the single EventContext parameter
gets invoked in any case, regardless of the activation context parameter
count (gets invoked with no activation context too!).

nice to see you working with tapestry, i know your name from the german
java scene and appreciate, that tapestry gets increasingly attention in
germany ;)

felix


Am 05.05.2012 01:13, schrieb Arno Haase:
>>> Thanks for the help - onActivate() at least looks better than
>>> all ideas I had come up with. It still does not seem like an
>>> elegant, intuitive solution to me.
> 
>> Why not elegant and intuitive? To me, they are. :) Just curious.
>> ;)
> 
> If a page has optional context parameters, my understanding is that
> the way to implement that is to write several onActivate() methods
> with different numbers of parameters.
> 
> Anyway, if there are several onActivate() methods they are all called,
> starting with the method with the most parameters. The only
> onActivate() method that is guaranteed to be called is the one with
> least parameters - in my code typically with no args.
> 
> If I want to provide default values for context parameters, the
> initialization code must check whether they are already initialized,
> and assign default values only if they were not initialized by one of
> the other onActivate() methods (see code below).
> 
> This even breaks in the (admittedly rare) case that one of the context
> parameters is coerced to null.
> 
> Placing the initialization in pageAttached() or some other method that
> is guaranteed to be called exactly once and before any other stuff
> gets done allows straight-forward assignment of default values with
> less potential for subtle errors.
> 
> Thanks for taking the time to look into this!
> 
> - Arno
> 
> 
> 
> class MyPage {
>   @Property String x;
> 
> 
>   public onActivate(String x) {
>     this.x = x;
>   }
> 
>   public onActivate() {
>     // this check is the part I find less than elegant
>     if (x == null) {
>       x = "default value";
>     }
>   }
> }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>> Thanks for the help - onActivate() at least looks better than
>> all ideas I had come up with. It still does not seem like an
>> elegant, intuitive solution to me.
> 
> Why not elegant and intuitive? To me, they are. :) Just curious.
> ;)

If a page has optional context parameters, my understanding is that
the way to implement that is to write several onActivate() methods
with different numbers of parameters.

Anyway, if there are several onActivate() methods they are all called,
starting with the method with the most parameters. The only
onActivate() method that is guaranteed to be called is the one with
least parameters - in my code typically with no args.

If I want to provide default values for context parameters, the
initialization code must check whether they are already initialized,
and assign default values only if they were not initialized by one of
the other onActivate() methods (see code below).

This even breaks in the (admittedly rare) case that one of the context
parameters is coerced to null.

Placing the initialization in pageAttached() or some other method that
is guaranteed to be called exactly once and before any other stuff
gets done allows straight-forward assignment of default values with
less potential for subtle errors.

Thanks for taking the time to look into this!

- - Arno



class MyPage {
  @Property String x;


  public onActivate(String x) {
    this.x = x;
  }

  public onActivate() {
    // this check is the part I find less than elegant
    if (x == null) {
      x = "default value";
    }
  }
}

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+kYrIACgkQbmZsMyUPuXSQgQCfSSUfNcvM34XVmHcosQu7x2DT
BPoAnR7V/7DjsQwyZB0fyvasOfF+UEX+
=xclp
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 04 May 2012 17:59:58 -0300, Arno Haase  
<ar...@haase-consulting.com> wrote:

> Thanks for the help - onActivate() at least looks better than all
> ideas I had come up with. It still does not seem like an elegant,
> intuitive solution to me.

Why not elegant and intuitive? To me, they are. :) Just curious. ;)

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks for the help - onActivate() at least looks better than all
ideas I had come up with. It still does not seem like an elegant,
intuitive solution to me.

- - Arno

Am 04.05.2012 21:27, schrieb Norman Franke:
> On May 4, 2012, at 12:52 PM, Thiago H. de Paula Figueiredo wrote:
> 
>> On Fri, 04 May 2012 12:31:26 -0300, Norman Franke
>> <no...@myasd.com> wrote:
>> 
>>> 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.
>> 
>> I'm sorry for not having the time to check this with code now,
>> but I don't think this is correct. As far as I know, onActivate()
>> is only invoked when the page is requested. onPassivate() is
>> invoked every time you generate an event link inside the page.
> 
> 
> I did check, and in 5.3.3 it doesn't seem to anymore. I'm pretty
> sure it was in 5.1.x, but never really checked recently.
> 
> -Norman
> 
> 
> 
> ---------------------------------------------------------------------
>
> 
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+kQ0kACgkQbmZsMyUPuXSzSACg0rSpTxVmz51gUk5twtOJdtGs
bVAAn3yfh0eH5Jw8re4HKDcujIO14QrU
=XX1y
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ah, I see - thanks!

Am 06.05.2012 14:37, schrieb Thiago H. de Paula Figueiredo:
> On Sun, 06 May 2012 02:41:13 -0300, Arno Haase 
> <ar...@haase-consulting.com> wrote:
> 
>> PS: What's the 'relative assets pitfall'?
> 
> Imagine you have a page which URL is /view/edit (the URL itself
> doesn't matter). Now imagine that you add an image to your page, it
> doesn't matter <img> or CSS, but I'll use <img> as an example:
> <img src="images/background.jpg"/>. This is a relative URL, so the
> browser will concatenate the page URL with image URL: 
> /view/edit/images/background.jpg. Tapestry interprets this as
> requesting the /view/edit page with 'images' and 'background.jpg'
> as the page activation context and the page onActivation() method
> is invoked, which is not what we want. Solution: use absolute
> paths: <img src="/images/background.jpg"/>
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+mgmMACgkQbmZsMyUPuXRCtgCgv71o2h7oiT3V93PCf+LlwBEb
FuYAoIyIbv2xng5PPkR4zeWMgwNtOrEI
=SpM1
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sun, 06 May 2012 02:41:13 -0300, Arno Haase  
<ar...@haase-consulting.com> wrote:

> PS: What's the 'relative assets pitfall'?

Imagine you have a page which URL is /view/edit (the URL itself doesn't  
matter). Now imagine that you add an image to your page, it doesn't matter  
<img> or CSS, but I'll use <img> as an example: <img  
src="images/background.jpg"/>. This is a relative URL, so the browser will  
concatenate the page URL with image URL: /view/edit/images/background.jpg.  
Tapestry interprets this as requesting the /view/edit page with 'images'  
and 'background.jpg' as the page activation context and the page  
onActivation() method is invoked, which is not what we want. Solution: use  
absolute paths: <img src="/images/background.jpg"/>

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks - I somehow missed when I scanned the Jumpstart page...

Am 07.05.2012 13:45, schrieb Geoff Callender:
> It's there - look for "AJAX: What is Called and When" in the 5th
> column of the home page.
> 
> http://jumpstart.doublenegative.com.au/jumpstart/
> 
> Cheers,
> 
> Geoff
> 
> On 06/05/2012, at 3:41 PM, Arno Haase wrote:
> 
> That is a great link, thanks for pointing it out. Is there also a
> page with the same information for AJAX calls? It is mentioned but
> I could not find it.
> 
> PS: What's the 'relative assets pitfall'?
> 
> Am 05.05.2012 04:59, schrieb Geoff Callender:
>>>> Just triple-checked the What Is Called And When page and it
>>>> is correct.
>>>> 
>>>> http://jumpstart.doublenegative.com.au/jumpstart/examples/navigation/whatiscalledandwhen
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> 
On 05/05/2012, at 8:41 AM, Thiago H. de Paula Figueiredo wrote:
>>>> 
>>>>> On Fri, 04 May 2012 16:27:44 -0300, Norman Franke 
>>>>> <no...@myasd.com> wrote:
>>>>> 
>>>>>>> I'm sorry for not having the time to check this with
>>>>>>> code now, but I don't think this is correct. As far as
>>>>>>> I know, onActivate() is only invoked when the page is
>>>>>>> requested. onPassivate() is invoked every time you
>>>>>>> generate an event link inside the page.
>>>>>> 
>>>>>> I did check, and in 5.3.3 it doesn't seem to anymore. I'm
>>>>>>  pretty sure it was in 5.1.x, but never really checked 
>>>>>> recently.
>>>>> 
>>>>> This hasn't changed at least since the 5.0.5 days, when I 
>>>>> started using Tapestry. What you've experienced was
>>>>> probably the relative assets (images, CSS) pitfall. So,
>>>>> people, please stop saying that onActivate() is invoked too
>>>>> much and to avoid using it to fetch data at all costs. ;)
>>>>> 
>>>>> -- Thiago H. de Paula Figueiredo Independent Java, Apache 
>>>>> Tapestry 5 and Hibernate consultant, developer, and
>>>>> instructor Owner, Ars Machina Tecnologia da Informação
>>>>> Ltda. http://www.arsmachina.com.br
>>>>> 
>>>>> ---------------------------------------------------------------------
>>>>>
>>>>>
>>>>>
>
>>>>> 
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>>> For additional commands, e-mail:
>>>>> users-help@tapestry.apache.org
>>>>> 
>>>> 
>>>> 
>>>> ---------------------------------------------------------------------
>>>>
>>>>
>>>>
>
>>>> 
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>>> For additional commands, e-mail:
>>>> users-help@tapestry.apache.org
>>>> 
>>>> 
>> 
>> ---------------------------------------------------------------------
>>
>> 
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+nuNYACgkQbmZsMyUPuXTr7QCg0WId50pltTOmDCajoM9YYVx3
8QYAn3JfNSQ2cHRq1OZhMAwQQ0DeduUu
=WW23
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
It's there - look for "AJAX: What is Called and When" in the 5th column of the home page.

	http://jumpstart.doublenegative.com.au/jumpstart/

Cheers,

Geoff

On 06/05/2012, at 3:41 PM, Arno Haase wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> That is a great link, thanks for pointing it out. Is there also a page
> with the same information for AJAX calls? It is mentioned but I could
> not find it.
> 
> PS: What's the 'relative assets pitfall'?
> 
> Am 05.05.2012 04:59, schrieb Geoff Callender:
>> Just triple-checked the What Is Called And When page and it is 
>> correct.
>> 
>> http://jumpstart.doublenegative.com.au/jumpstart/examples/navigation/whatiscalledandwhen
>> 
>> 
>> 
>> 
>> On 05/05/2012, at 8:41 AM, Thiago H. de Paula Figueiredo wrote:
>> 
>>> On Fri, 04 May 2012 16:27:44 -0300, Norman Franke 
>>> <no...@myasd.com> wrote:
>>> 
>>>>> I'm sorry for not having the time to check this with code 
>>>>> now, but I don't think this is correct. As far as I know, 
>>>>> onActivate() is only invoked when the page is requested. 
>>>>> onPassivate() is invoked every time you generate an event 
>>>>> link inside the page.
>>>> 
>>>> I did check, and in 5.3.3 it doesn't seem to anymore. I'm 
>>>> pretty sure it was in 5.1.x, but never really checked 
>>>> recently.
>>> 
>>> This hasn't changed at least since the 5.0.5 days, when I
>>> started using Tapestry. What you've experienced was probably the
>>> relative assets (images, CSS) pitfall. So, people, please stop
>>> saying that onActivate() is invoked too much and to avoid using
>>> it to fetch data at all costs. ;)
>>> 
>>> -- Thiago H. de Paula Figueiredo Independent Java, Apache 
>>> Tapestry 5 and Hibernate consultant, developer, and instructor 
>>> Owner, Ars Machina Tecnologia da Informação Ltda. 
>>> http://www.arsmachina.com.br
>>> 
>>> ---------------------------------------------------------------------
>>> 
>>> 
>>> 
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>>> For additional commands, e-mail: users-help@tapestry.apache.org
>>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> 
>> 
>> 
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
>> 
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
> 
> iEYEARECAAYFAk+mDvQACgkQbmZsMyUPuXQINQCgpwO5iNzt0XtVb3ukuGP0RlNe
> 5eEAn09qlWT7AWT94kHCnYh4St98t41z
> =lH1y
> -----END PGP SIGNATURE-----
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

That is a great link, thanks for pointing it out. Is there also a page
with the same information for AJAX calls? It is mentioned but I could
not find it.

PS: What's the 'relative assets pitfall'?

Am 05.05.2012 04:59, schrieb Geoff Callender:
> Just triple-checked the What Is Called And When page and it is 
> correct.
> 
> http://jumpstart.doublenegative.com.au/jumpstart/examples/navigation/whatiscalledandwhen
>
>
> 
> 
> On 05/05/2012, at 8:41 AM, Thiago H. de Paula Figueiredo wrote:
> 
>> On Fri, 04 May 2012 16:27:44 -0300, Norman Franke 
>> <no...@myasd.com> wrote:
>> 
>>>> I'm sorry for not having the time to check this with code 
>>>> now, but I don't think this is correct. As far as I know, 
>>>> onActivate() is only invoked when the page is requested. 
>>>> onPassivate() is invoked every time you generate an event 
>>>> link inside the page.
>>> 
>>> I did check, and in 5.3.3 it doesn't seem to anymore. I'm 
>>> pretty sure it was in 5.1.x, but never really checked 
>>> recently.
>> 
>> This hasn't changed at least since the 5.0.5 days, when I
>> started using Tapestry. What you've experienced was probably the
>> relative assets (images, CSS) pitfall. So, people, please stop
>> saying that onActivate() is invoked too much and to avoid using
>> it to fetch data at all costs. ;)
>> 
>> -- Thiago H. de Paula Figueiredo Independent Java, Apache 
>> Tapestry 5 and Hibernate consultant, developer, and instructor 
>> Owner, Ars Machina Tecnologia da Informação Ltda. 
>> http://www.arsmachina.com.br
>> 
>> ---------------------------------------------------------------------
>>
>>
>> 
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
> 
> 
> ---------------------------------------------------------------------
>
>
> 
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 
> 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+mDvQACgkQbmZsMyUPuXQINQCgpwO5iNzt0XtVb3ukuGP0RlNe
5eEAn09qlWT7AWT94kHCnYh4St98t41z
=lH1y
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
Just triple-checked the What Is Called And When page and it is correct.

	http://jumpstart.doublenegative.com.au/jumpstart/examples/navigation/whatiscalledandwhen


On 05/05/2012, at 8:41 AM, Thiago H. de Paula Figueiredo wrote:

> On Fri, 04 May 2012 16:27:44 -0300, Norman Franke <no...@myasd.com> wrote:
> 
>>> I'm sorry for not having the time to check this with code now, but I don't think this is correct. As far as I know, onActivate() is only invoked when the page is requested. onPassivate() is invoked every time you generate an event link inside the page.
>> 
>> I did check, and in 5.3.3 it doesn't seem to anymore. I'm pretty sure it was in 5.1.x, but never really checked recently.
> 
> This hasn't changed at least since the 5.0.5 days, when I started using Tapestry. What you've experienced was probably the relative assets (images, CSS) pitfall. So, people, please stop saying that onActivate() is invoked too much and to avoid using it to fetch data at all costs. ;)
> 
> -- 
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Mon, 07 May 2012 17:00:10 -0300, Norman Franke <no...@myasd.com> wrote:

> What is the relative assets pitfall?

Here's an explanation I gave in this thread:  
http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5689193.html

> I reference all assets like "context:foo/css/bar.css" in my @Imports. I  
> guess I'll keep using onActivate() since it works.

This works without problems because it's Tapestry who's generating the  
URLs, so it generates absolute paths or correct relative paths, in other  
words, correct URLs.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 04 May 2012 16:27:44 -0300, Norman Franke <no...@myasd.com> wrote:

>> I'm sorry for not having the time to check this with code now, but I  
>> don't think this is correct. As far as I know, onActivate() is only  
>> invoked when the page is requested. onPassivate() is invoked every time  
>> you generate an event link inside the page.
>
> I did check, and in 5.3.3 it doesn't seem to anymore. I'm pretty sure it  
> was in 5.1.x, but never really checked recently.

This hasn't changed at least since the 5.0.5 days, when I started using  
Tapestry. What you've experienced was probably the relative assets  
(images, CSS) pitfall. So, people, please stop saying that onActivate() is  
invoked too much and to avoid using it to fetch data at all costs. ;)

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Norman Franke <no...@myasd.com>.
On May 4, 2012, at 12:52 PM, Thiago H. de Paula Figueiredo wrote:

> On Fri, 04 May 2012 12:31:26 -0300, Norman Franke <no...@myasd.com> wrote:
> 
>> 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.
> 
> I'm sorry for not having the time to check this with code now, but I don't think this is correct. As far as I know, onActivate() is only invoked when the page is requested. onPassivate() is invoked every time you generate an event link inside the page.


I did check, and in 5.3.3 it doesn't seem to anymore. I'm pretty sure it was in 5.1.x, but never really checked recently.

-Norman



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 04 May 2012 12:31:26 -0300, Norman Franke <no...@myasd.com> wrote:

> 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.

I'm sorry for not having the time to check this with code now, but I don't  
think this is correct. As far as I know, onActivate() is only invoked when  
the page is requested. onPassivate() is invoked every time you generate an  
event link inside the page.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
Or perhaps more elegant:  have beaneditor "recognize/allow" plain HTML
customization within its tags.  Right now (I tested this) if you enclose a
form with bean editor tags - it simply ignores its enclosed contents. 
Instead it should render only fields not customized within its open and
close tags. 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5696224.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
Got it.  Thanks, Geoff.   Sounds right, I was reaching the same conclusion.  

As for instantiating person, I guess it is the best practice  - even though
beaneditor covers the null case.  Most likely the user will start with
beaneditor in dev mode, transitioning to total control in production - which
is when those two event handlers become necessary - so might as well provide
from the get go.  

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5696047.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
Favour using the activation context. If person has been persisted to the database then pass the id in the activation context. Otherwise go with the technique you've suggested below.

	http://jumpstart.doublenegative.com.au/jumpstart/examples/state/passingdatabetweenpages

And yes, in my earlier response I forgot to instantiate a person in onPrepareForSubmit() when the mode is "create", not "update".

Geoff

On 09/05/2012, at 6:28 AM, netdawg wrote:

> Thanks for verifying.  Which brings me back to...the basic misunderstanding I
> seem to have on using persistence....
> 
> What is the best practice to HANDLE a parameter (say person) between pages
> or even the page submitting to itself?  That is, if not through some form
> form of session persistence?  Is it through intricate knowledge of these
> event handlers like onPrepareXxxx...?   Environment push/pop?  
> 
> Right now, if page1 submits person to page2.  
> 
> I am doing the following in Page1.class
> 
> @Inject
> Page2 page2
> 
> onSuccess method
> 1.  page2.setPerson(person)
> 2.  return page2;
> 
> Within Page2.class
> 
> @Persist (PersistenceConstants.FLASH)
> private Person person;
> provide setter for person, needed in page1
> 
> It works, of course, but is there a better way?
> 
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5695818.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 08 May 2012 17:28:19 -0300, netdawg <ne...@yahoo.com> wrote:

> Thanks for verifying.  Which brings me back to...the basic  
> misunderstanding I seem to have on using persistence....
>
> What is the best practice to HANDLE a parameter (say person) between  
> pages or even the page submitting to itself?

Page activation context (if you have one or couple parameters) or query  
parameters (otherwise). Of course, page activation context will only work  
if the object is store somewhere. If not, you'll need session persistence,  
flash or not.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
Thanks for verifying.  Which brings me back to...the basic misunderstanding I
seem to have on using persistence....

What is the best practice to HANDLE a parameter (say person) between pages
or even the page submitting to itself?  That is, if not through some form
form of session persistence?  Is it through intricate knowledge of these
event handlers like onPrepareXxxx...?   Environment push/pop?  

Right now, if page1 submits person to page2.  

I am doing the following in Page1.class

@Inject
Page2 page2

onSuccess method
1.  page2.setPerson(person)
2.  return page2;

Within Page2.class

@Persist (PersistenceConstants.FLASH)
private Person person;
provide setter for person, needed in page1

It works, of course, but is there a better way?



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5695818.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
I favour consistency too, but I'd go the other way: beaneditor should catch the NPE and throws an exception that explains the principal involved, ie. that it is up to you to provide initial object(s) that the form will overlay with its input and hidden fields.

The thing is, a Form may relate to one entity, several entities, and/or fields that are complexly derived from/to entity fields.

I don't see a benefit in having Tapestry guess how to set up an object during Create.

On 09/05/2012, at 10:16 AM, netdawg wrote:

> I would think JIRA ... . 
> 
> Form event handling to be consistent with beaneditor: preferably upgrade
> form tag to have an "objects" property.  All form objects should be null
> proof, by default, during render, submit.  This will reduce code in page
> class...especially for the simple case of choosing between
> pageActivationContext(s) and empty object(s). 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5696218.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
I would think JIRA ... . 

Form event handling to be consistent with beaneditor: preferably upgrade
form tag to have an "objects" property.  All form objects should be null
proof, by default, during render, submit.  This will reduce code in page
class...especially for the simple case of choosing between
pageActivationContext(s) and empty object(s). 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5696218.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 08 May 2012 18:44:30 -0300, Geoff Callender  
<ge...@gmail.com> wrote:

> Why does BeanEditor do that? To me it sounds like a recipe for confusion!

Probably to avoid an NPE during rendering and form submission.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
Why does BeanEditor do that? To me it sounds like a recipe for confusion!

Geoff

On 09/05/2012, at 5:49 AM, Thiago H. de Paula Figueiredo wrote:

> On Tue, 08 May 2012 16:44:10 -0300, netdawg <ne...@yahoo.com> wrote:
> 
>> You are right, though, about person being null in both cases - verified. I was thinking just total control did that.  Both do.  However, total control seems to need the person=new Person in onPrepareForSubmit.
> 
> Yes. No component knows which is the object being edited and, in addition, a Form could be editing many objects at the same time.
> 
>> Beaneditor seams to work with or without that null check.  So, bottom line, there is a
>> difference.
> 
> Oops, you're right in this case. BeanEditor does instance the object if it's null when rendering or handling a form submission. Thanks for the correction. :)
> 
> -- 
> Thiago H. de Paula Figueiredo
> Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor
> Owner, Ars Machina Tecnologia da Informação Ltda.
> http://www.arsmachina.com.br
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 08 May 2012 16:44:10 -0300, netdawg <ne...@yahoo.com> wrote:

> You are right, though, about person being null in both cases - verified.  
> I was thinking just total control did that.  Both do.  However, total  
> control seems to need the person=new Person in onPrepareForSubmit.

Yes. No component knows which is the object being edited and, in addition,  
a Form could be editing many objects at the same time.

> Beaneditor seams to work with or without that null check.  So, bottom  
> line, there is a
> difference.

Oops, you're right in this case. BeanEditor does instance the object if  
it's null when rendering or handling a form submission. Thanks for the  
correction. :)

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
Should be easy enough to verify, Thiago ( BTW, "total control edit" is
Jumpstart lingo).  

Case 1:  Total control,  create will fail. 
Case 2:  BeanEditor,  create will work.  

You are right, though, about person being null in both cases - verified.  I
was thinking just total control did that.  Both do.  However, total control
seems to need the person=new Person in onPrepareForSubmit.  Beaneditor seams
to work with or without that null check.  So, bottom line, there is a
difference.  

  



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5695743.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 08 May 2012 00:13:49 -0300, netdawg <ne...@yahoo.com> wrote:

> Curious:  IF no persistence, not even flash, is to be used for person,  
> THEN ,
> how would we handle the case of person being NULL during  
> onPrepareForSubmit?

Just do this.person = new Person(). Of course, this will only work if all  
fields that will be sent to the data store are being edited by form fields.

>
> Specifically,
>
>   void onPrepareForSubmit()
>   {
>     if (this.person == null)
>     {
> /////////////////////////////////////////////////////////////////////////////////////////////////
>        //  if not persisting person, will come here during Create person
>        //  What can be done here?  Or, how to avoid coming here  (w/o
> persistence)?
>        //  person does not have Id yet to find it by service
>        //  MUST handle this to avoid submit error -
>        //  logs indicate empty person in onPrepareForRender is already  
> null
> by this stage
>        //  form data may still be in session(?), but no person object
> available to bind it
>     }
>     else
>     {
> //////////////////////////////////////////////////////////////////////////////////////////////
>        //  this is what happens normally in Update Person
>        //  Using persistence, even the minimal flash persist,
>        //  will allow coming here as well in Create Person
>        //  Either way nothing to be done here since person is already in
> session,
>        //  either  thanks to @PageActivationContext (update) or
>        //  thanks to onPrepareForRender empty person (create)
>     }
>   }
>
> Conclusion (possibly wrong?):  Some form of persistence is essential - to
> support create - when onPrepareForRender sets up empty person.   Use  
> flash
> persist annotation -
>
> @Persist (PersistenceConstants.FLASH)
> private Person person
>
> --
> View this message in context:  
> http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5692646.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>


-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
Consultor, desenvolvedor e instrutor em Java, Tapestry e Hibernate
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 08 May 2012 00:40:21 -0300, netdawg <ne...@yahoo.com> wrote:

> And, BTW, question above applies to "total control form" - i.e. not  
> based on beaneditor.

How many time the list will tell you that using BeanEditor or BeanEditForm  
or what you call a total control form has absolutely no difference  
regarding the need or not of persistence until you get it?

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
And, BTW, question above applies to "total control form" - i.e. not based on
beaneditor.

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5692665.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
Curious:  IF no persistence, not even flash, is to be used for person, THEN ,
how would we handle the case of person being NULL during onPrepareForSubmit?  

Specifically, 

  void onPrepareForSubmit()
  {
    if (this.person == null) 
    {
      
/////////////////////////////////////////////////////////////////////////////////////////////////
       //  if not persisting person, will come here during Create person 
       //  What can be done here?  Or, how to avoid coming here  (w/o
persistence)?
       //  person does not have Id yet to find it by service
       //  MUST handle this to avoid submit error - 
       //  logs indicate empty person in onPrepareForRender is already null
by this stage 
       //  form data may still be in session(?), but no person object
available to bind it  
    }
    else 
    {
      
//////////////////////////////////////////////////////////////////////////////////////////////
       //  this is what happens normally in Update Person 
       //  Using persistence, even the minimal flash persist, 
       //  will allow coming here as well in Create Person
       //  Either way nothing to be done here since person is already in
session, 
       //  either  thanks to @PageActivationContext (update) or 
       //  thanks to onPrepareForRender empty person (create)
    }
  }

Conclusion (possibly wrong?):  Some form of persistence is essential - to
support create - when onPrepareForRender sets up empty person.   Use flash
persist annotation - 

@Persist (PersistenceConstants.FLASH)  
private Person person

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5692646.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
No need to persist. The form holds the person's data, and submits it, and since you are doing Ajax calls there is no redirect - it goes VALIDATE, SUCCESS, PREPARE_FOR_RENDER, PREPARE all in the same request - so the person data is still available in onPrepareForRender and goes round trip back out to the form.

If you're doing create and update with the same page, try (untested code):

	private boolean inited;

	void onPrepareForRender() {

		if (!inited) {
			if (person.getId() == null) {
				// set up empty person initial data
			}
			else {
				// Get person for the form fields to overlay
				person = personService.findPerson(person);
			}
			inited = true;
		}

	}

	void onPrepareForSubmit() {

		if (person.getId() != null) {
			// Get person for the form fields to overlay
			person = personService.findPerson(person);
		}

	}

On 06/05/2012, at 12:40 PM, netdawg wrote:

> Yep, second Geoff.  That is what I am doing - using onPrepareForRender().  
> 
> Say specialized form is EditPerson.tml
> 
> <form t:type="Form" t:id="person" >
>  <t:errors />
> 
>  [various form elements]
> 
> </form>
> </html>
> 
> In the corresponding EditPerson.java, this what I have:
> 
> /**
>   *  Enables reuse of Edit as Create
>   */
>  void onPrepareForRender()
>  {
>    if (this.person == null) 
>    {
>      this.person = new Person();  // Person with defaults - populates the
> form
>    }
>    else
>    {
>       // nothing to do - form will pick up persisted person object 
>    }
>  }
> 
> I am posting because this probably most directly addresses your question -
> initialize with default values(?)  Not sure if it is the most correct or
> elegant or what the side effects are - good or bad.  One being that the
> person object needs to be persisted in the Session (for submit to work). 
> Whereas using the beaneditor & co, you just need to use activationContext
> and the person is initialized by just the Id.   
> 
> Thoughts?
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5688725.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by netdawg <ne...@yahoo.com>.
Yep, second Geoff.  That is what I am doing - using onPrepareForRender().  

Say specialized form is EditPerson.tml

<form t:type="Form" t:id="person" >
  <t:errors />

  [various form elements]
   
</form>
</html>

In the corresponding EditPerson.java, this what I have:

 /**
   *  Enables reuse of Edit as Create
   */
  void onPrepareForRender()
  {
    if (this.person == null) 
    {
      this.person = new Person();  // Person with defaults - populates the
form
    }
    else
    {
       // nothing to do - form will pick up persisted person object 
    }
  }

I am posting because this probably most directly addresses your question -
initialize with default values(?)  Not sure if it is the most correct or
elegant or what the side effects are - good or bad.  One being that the
person object needs to be persisted in the Session (for submit to work). 
Whereas using the beaneditor & co, you just need to use activationContext
and the person is initialized by just the Id.   

Thoughts?

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Best-place-to-initialize-form-data-tp5685603p5688725.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Geoff Callender <ge...@gmail.com>.
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
> 
> 
> 


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 04 May 2012 12:31:26 -0300, Norman Franke <no...@myasd.com> wrote:

> 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
>
>
>


-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
Consultor, desenvolvedor e instrutor em Java, Tapestry e Hibernate
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by Norman Franke <no...@myasd.com>.
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




Re: Best place to initialize form data

Posted by Arno Haase <ar...@haase-consulting.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

>> 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?

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+j0UgACgkQbmZsMyUPuXSm6wCg5u/kipZWwTLRoctYo9t01cEy
4wgAoMV7c0msm2Xp5YeEcnDX3tJWKsIF
=NdrG
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Best place to initialize form data

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 04 May 2012 08:43:27 -0300, Arno Haase  
<ar...@haase-consulting.com> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hi everyone,
>
> this may be an obvious newbie question, but I have found no answer
> that looks good to me. Anyway, here goes:
>
> What is the best place to initialize a property with a default value
> for a form?

A method handling the prepare event from Form:

onPrepare() { ... }

@OnEvent("prepare")
anyMethodName() { ... }

> 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).

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org