You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Ulrich VACHON <u....@gmail.com> on 2011/07/19 18:55:23 UTC

Multiple onActive

Hi all,

I would to know if it's possible to have several onActivate methods with
differents parameters type. In fact, I have two objects class with this
hierarchy :

       MySuperObject
            ^        ^
           /            \
  MyObject1     MyObject2

And I want to navigate with any objects and have different behavior in page,
like:

onActive(MyObject1 myObject1) {
   // some works with myObject1 instance...
}

onActive(MyObject2 myObject2) {
   // some works with myObject2 instance...
}

Unfortunately I have a ClassCastException...

An idea?

Regards,
Ulrich


--
View this message in context: http://tapestry.1045711.n5.nabble.com/Multiple-onActive-tp4612951p4612951.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: Multiple onActive

Posted by Ulrich VACHON <u....@gmail.com>.
Thx Robert for your reply, I suspected that it was not possible. I like your
solution but I will try to play with URL and have two methods.

Thx a lot,

Ulrich

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Multiple-onActivate-tp4612951p4613739.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: Multiple onActive

Posted by Robert Zeigler <ro...@roxanemy.com>.
You could supply a custom ValueEncoder for MySuperObject that would handle either case and make your code agnostic to the type of object.

You could use:
  onActivate(EventContext context)

And then grab the parameter values and convert as desired using the "get" method (but that will require you to have some knowledge of what type of object it is). One possibility would be to encode two values into the context, something like (untested code; in particular, the generics are off):

List<Object> onPassivate() {
   return Arrays.asList(myObject.getClass().getSimpleName(),myObject);
}

onActivate(EventContext context) {//I'm assuming that the objects are entities and are being encoded to an integer or long id...
  Class desiredType = ...//some code to generate the type.  It could be as simple as: "MyObject1".equals(context.get(String.class,0))?MyObject1.class:MyObject2.class 
  MySuperObject myObject = context.get(desiredType, 1);
  //do stuff
}

But there's not a way to do what you're asking directly.  Activate event handler precedence is determined by the number of parameters.  Tapestry tries to match the number of context parameters with the number of parameters in an activate handler, then attempts to coerce the string parameter values into the types specified in the activate method signature.  So you can't have two onActivate methods with a single method parameter because there's no way for Tapestry to know which one to use: it will pick one and attempt to coerce the parameter to said type.

Robert

On Jul 19, 2011, at 7/1911:55 AM , Ulrich VACHON wrote:

> Hi all,
> 
> I would to know if it's possible to have several onActivate methods with
> differents parameters type. In fact, I have two objects class with this
> hierarchy :
> 
>       MySuperObject
>            ^        ^
>           /            \
>  MyObject1     MyObject2
> 
> And I want to navigate with any objects and have different behavior in page,
> like:
> 
> onActive(MyObject1 myObject1) {
>   // some works with myObject1 instance...
> }
> 
> onActive(MyObject2 myObject2) {
>   // some works with myObject2 instance...
> }
> 
> Unfortunately I have a ClassCastException...
> 
> An idea?
> 
> Regards,
> Ulrich
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Multiple-onActive-tp4612951p4612951.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