Hi Omar,

> I am trying to dynamically load a component which must be determined at
> run time
Do you need to define the component at runtime, or simply choose the component 
at runtime? For most cases, and I suspect yours too, all you need to do is 
choose from a number of existing components, if so then it is possible to do 
exactly what you require, but with a different approach. 

If you get your head around how composition works in Tapestry then its not so 
daunting. Blocks don't need to be defined in any particular order, they can be 
reused any number of times and precise rendering can be controlled. The 
solution I used was to create an 'assembly component' that assembles components 
via a tree, it works great and eliminates all those if / else's. 

But for any solution consider that somewhere you need to define components, and 
somehow choose them, whatever approach you use, you can't really escape this, 
so why not do this all in the same place? If you require recursion then do it 
in Java and assemble a tree to iterate over in your UI. Its not very much more 
work, just a different approach that also has the advantage that most of the 
core logic is done in Java so can be examined with a debugger. If you need more 
control over rendering then use the HeartBeat Environmental service, though I 
don't know of many good examples.

cheers,
Peter



----- Original Message -----
From: "Omar Carvajal" <[email protected]>
To: "Tapestry users" <[email protected]>
Sent: Tuesday, 5 October, 2010 07:19:27 GMT +02:00 Athens, Beirut, Bucharest, 
Istanbul
Subject: Re: Dynamically loading a component

This is horrible news since I have no way of knowing the components that needs 
to be called at design time (other wise I would use a <t:block>).

There is no magic that can be done to achieve this at all? Even if I have to 
use the internal services.

Thanks for your quick response.

Omar

On Oct 4, 2010, at 18:41, "Thiago H. de Paula Figueiredo" <[email protected]> 
wrote:

> On Mon, 04 Oct 2010 19:35:38 -0300, Omar Carvajal 
> <[email protected]> wrote:
> 
>> Hey all,
> 
> Hi!
> 
>> I am trying to dynamically load a component which must be determined at
>> run time, I figured I must use a t:delegate to render the component.
>> On the Java side of things I cannot figure out how to instantiate the
>> actual component. I saw the @Component annotation with the "type" parameter 
>> but this does not let me dynamically load a component from a function.
>> Does anybody have any idea on how to do this?
> 
> Declare the components inside a <t:block>. <t:block> are not rendered unless 
> you pass it to a Delegate or return it to update a Zone. You cannot 
> instantiate components in Java code, as Tapestry is static structure, dynamic 
> behavior (including rendering). Take a look at 
> http://tapestry.apache.org/tapestry5.1/cookbook/switch.html. It shows you how 
> to render blocks dynamically, but the same approach can be used for 
> components as well. You'll use @InjectComponent to get access in your Java 
> code to the component instances declared in templates.
> 
> -- 
> 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: [email protected]
> For additional commands, e-mail: [email protected]
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to