[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tapestry-user
Subject:    Re: Component with mixed functionality?
From:       Matt Brock <brockmatt () gmail ! com>
Date:       2007-12-16 1:13:27
Message-ID: 14357097.post () talk ! nabble ! com
[Download RAW message or body]


hbf wrote:
> 
> The problem is: the 
 already is a component (@If) but I need to
> attach additional behaviour (class should alternate between "even" and
> "odd"). What's an easy way to accomplish this?
> 
The problem is that I don't think your even/odd requirement will work with
any of the regular Tapestry components.  Normally you iterate over a
collection of values (the @For component, for example), and use an OGNL
expression like class="ognl:(index % 2 == 0 ? 'even' : 'odd')" to determine
whether the row is odd or even.  But the problem is, not all of your rows
are going to be rendered.  So what happens when the first row's @If
condition evaluates true, the second row's condition evaluates false, then
the third evaluates true?  You'll end up with two rows both marked "even".

The easiest way to do this would be to write your own renderComponent
method.  That way you get complete control over the output.  This has the
added advantage of bypassing the OGNL parser.  You don't need a component
definition file (.jwc) or an HTML file, either.  Just a single java class. 
Something like this:

MyComponent.java

public abstract class MyComponent extends AbstractComponent {

&nbsp;&nbsp;@Parameter
&nbsp;&nbsp;public abstract MyObject getMyObject();

&nbsp;&nbsp;protected void renderComponent(IMarkupWriter writer,
IRequestCycle cycle) {
// You can also use the writer.begin/writer.attribute way of doing things,
but this is faster.
&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer output = new StringBuffer(); 
&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;table&gt;"); 
&nbsp;&nbsp;&nbsp;&nbsp;int i = 0;
&nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getTitle() != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append(i % 2 ==
0 ? " class=\"even\"" :
"").append("&gt;&lt;td&gt;").append(getMyObject().getTitle()).append("&lt;/td&gt;&lt;/tr&gt;");
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;if (getMyObject().getNextProperty() != null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output.append("&lt;tr").append(i % 2 ==
0 ? " class=\"even\"" :
"").append("&gt;&lt;td&gt;").append(getMyObject().getNextProperty()).append("&lt;/td&gt;&lt;/tr&gt;");
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;...etc...
&nbsp;&nbsp;&nbsp;&nbsp;output.append("
");
&nbsp;&nbsp;&nbsp;&nbsp;writer.print(output,true);
&nbsp;&nbsp;}
}

PageThatUsesComponent.html

&lt;span jwcid="@MyComponent" myObject="ognl:somePageObject" /&gt;


-- 
View this message in context: \
http://www.nabble.com/Component-with-mixed-functionality--tp14270320p14357097.html \
Sent from the Tapestry - User mailing list archive at Nabble.com.



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic