You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Piero Sartini <li...@pierosartini.de> on 2009/06/18 23:29:30 UTC

Page event after activation?

Hi there.

Is there a lifecycle method that gets called after a page is activated?
My usecase is in an abstract page that defines some common events I want to be 
able to call form every page.

AbstractPage -> MyPage

Now, in AbstractPage there is also some common logic that is equal for all 
pages. For example I need to change the included stylesheet based on some 
conditions.

For this purpose I created an abstract method that my pages need to provide. 
(getUser() for example). This all works perfect - BUT my pages should be able 
to use different activation contexts and use this information to provide their 
implementation of getUser().

Now - I can't use pageAttached because it is called before activation. I could 
use onActivate() but can't be sure my pages don't overwrite it or need 
different contexts.

So is there some lifecycle method to execute code after activation?
Or is there some simpler solution? Thanks for any pointers.

	Piero


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


Re: Page event after activation?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Fri, 19 Jun 2009 13:42:58 -0300, Piero Sartini <li...@pierosartini.de>  
escreveu:

> Ok, finally got it! Thank you very much! :-)

You're welcome! EventContext is a little gem that could have more emphasys  
in the documentation . . .

-- 
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago

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


Re: Page event after activation?

Posted by Piero Sartini <li...@pierosartini.de>.
> My solution cover everything you needed without the need for
> subclasses to call initialize(). About some pages needing two
> parameters, other two, this isn't a problem: just use EventContext as
> the parameter of your onActivate(). Regardless of how many parameters
> were used, it will be called. You can check the number of parameters
> using EventContext.getCount() and get their values using
> EventContext(Class desiredType, int index).

Ok, finally got it! Thank you very much! :-)

	Piero

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


Re: Page event after activation?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, Jun 19, 2009 at 5:49 AM, Piero Sartini<li...@pierosartini.de> wrote:
> I thought about that as well - and the problem with that solution is that my
> base class does not know about the activation context of its childs. Some
> pages need two parameters, some need only one - sometimes it is an ID of
> something, sometime it is the username. Its totally in the control of the page
> how they initialize themselve.

My solution cover everything you needed without the need for
subclasses to call initialize(). About some pages needing two
parameters, other two, this isn't a problem: just use EventContext as
the parameter of your onActivate(). Regardless of how many parameters
were used, it will be called. You can check the number of parameters
using EventContext.getCount() and get their values using
EventContext(Class desiredType, int index).

> For know I am calling initialize() in the onActivate(...) method of every
> page. But my feeling is that there should  be some framework provided
> initialization method that is called AFTER page activation.

There's no need for that.

> Is this still something that doesnt make sense to you? (just to be sure I am
> not on the completely wrong track).

It doesn't make any sense, because the solution I gave you solves all
your problems in an elegant way. Please try the solution.

-- 
Thiago

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


Re: Page event after activation?

Posted by Piero Sartini <li...@pierosartini.de>.
Am Freitag, 19. Juni 2009 02:30:45 schrieb Thiago H. de Paula Figueiredo:
> As far as I can see, this is not a matter of Tapestry events, but of
> implementing a Template design pattern. ;)
>
> 	final Object onActivate(EventContext context) {
> 		initialize(context);
> 	}
>
> 	protected void initialize(EventContext context) {
> 		this.statusmessage = getUser().getStatusMessage();
> 	}

I thought about that as well - and the problem with that solution is that my 
base class does not know about the activation context of its childs. Some 
pages need two parameters, some need only one - sometimes it is an ID of 
something, sometime it is the username. Its totally in the control of the page 
how they initialize themselve.

For know I am calling initialize() in the onActivate(...) method of every 
page. But my feeling is that there should  be some framework provided 
initialization method that is called AFTER page activation.

pageActivated() comes to mind. 

Is this still something that doesnt make sense to you? (just to be sure I am 
not on the completely wrong track).

	Piero



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


Re: Page event after activation?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Thu, 18 Jun 2009 20:22:53 -0300, Piero Sartini <li...@pierosartini.de>  
escreveu:

> If I overwrite the onActivate() method and put my code there, this code  
> that needs to be executed on every page gets overwritten as well (and I  
> have to duplicate it on every page).
> One solution would be to create an initialize() method that needs to be  
> called by every page at the end of their onActivate(). But I hoped for a  
> cleaner
> solution :-)

As far as I can see, this is not a matter of Tapestry events, but of  
implementing a Template design pattern. ;)

abstract class BasePage() {

	@Property private String statusmessage;

	protected abstract User getUser();

	final Object onActivate(EventContext context) {
		...
		initialize(context);
		...
	}

	protected void initialize(EventContext context) {
		this.statusmessage = getUser().getStatusMessage();
	}

}

class MyPage() extends BasePage {
	private User user;
	public void initialize(EventContext context) {
		user = repository.findByUsername(context.get(String.class, 0));
	}

	protected User getUser() {
		return user;
	}
}

-- 
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago

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


Re: Page event after activation?

Posted by Piero Sartini <li...@pierosartini.de>.
> Each class can have as many onActivate() methods as you want. But I would
> have a single onActivate(EventContext context) method and let the
> subclasses override it. Or define a method like process(EventContext
> context) and call it from a final onActivate(EventContext) method.

If I overwrite the onActivate() method and put my code there, this code that 
needs to be executed on every page gets overwritten as well (and I have to 
duplicate it on every page).

One solution would be to create an initialize() method that needs to be called 
by every page at the end of their onActivate(). But I hoped for a cleaner 
solution :-)

Example:
I hoped for an automatic way to call the initialize() method. In onActivate it 
doesnt make sense because maybe MyPage2 wont use the username as activation 
context but something else. And if any page overwrites the activation context 
I am getting problems as well.

BasePage.java:
---------------------------------------------------------------------
abstract class BasePage() {

@Property private String statusmessage;

initialize() {
	this.statusmessage = getUser().getStatusMessage();
}

abstract User getUser();
}

}
--------------------------------------------------------------
class MyPage() extends BasePage {
private User user;
onActivate(String username) {
 user = repository.findByUsername(username);
}

User getUser() {
return user;
}
--------------------------------------------------------------


	Piero


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


Re: Page event after activation?

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Thu, 18 Jun 2009 18:29:30 -0300, Piero Sartini <li...@pierosartini.de>  
escreveu:

> AbstractPage -> MyPage
>
> Now, in AbstractPage there is also some common logic that is equal for  
> all pages. For example I need to change the included stylesheet based on  
> some
> conditions.

> For this purpose I created an abstract method that my pages need to  
> provide. (getUser() for example). This all works perfect - BUT my pages  
> should be able to use different activation contexts and use this  
> information to provide their implementation of getUser().

Each class can have as many onActivate() methods as you want. But I would  
have a single onActivate(EventContext context) method and let the  
subclasses override it. Or define a method like process(EventContext  
context) and call it from a final onActivate(EventContext) method.

-- 
Thiago H. de Paula Figueiredo
Independent Java consultant, developer, and instructor
http://www.arsmachina.com.br/thiago

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