Author: hlship
Date: Tue Apr 28 21:20:24 2009
New Revision: 769546
URL: http://svn.apache.org/viewvc?rev=769546&view=rev
Log:
TAP5-531: Add cookbook documentation to show how to use the delegate component
for switch-like behaviour
Added:
tapestry/tapestry5/trunk/src/site/apt/cookbook/switch.apt (with props)
Modified:
tapestry/tapestry5/trunk/src/site/site.xml
Added: tapestry/tapestry5/trunk/src/site/apt/cookbook/switch.apt
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/src/site/apt/cookbook/switch.apt?rev=769546&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/src/site/apt/cookbook/switch.apt (added)
+++ tapestry/tapestry5/trunk/src/site/apt/cookbook/switch.apt Tue Apr 28
21:20:24 2009
@@ -0,0 +1,79 @@
+ ---
+ Switching Cases
+ ---
+
+Switching Cases
+
+ With Tapestry's <<<If>>> component you can only test one condition at a
time. In order to distinguish multiple cases,
+ you'd have to write complex nested if/else constructs in your page template
and have a checker method for each
+ test inside your page class.
+
+ In cases where you have to distinguish multiple cases, the <<<Delegate>>>
component comes in. It delegates rendering
+ to some other component, for example a <<<Block>>>. For each case you have,
you basically wrap the content inside a
+ <<<Block>>> that doesn't get rendered by default. You then place a Delegate
component on your page and point it to
+ a method inside your page class that will decide which of your Blocks should
be rendered.
+
+ Imagine for example a use case, where you want to distinguish between 4
cases and you have an int property called
+ <<<whichCase>>> that should be tested against. Your page template would look
as follows:
+
+----
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
+ <body>
+ <h1>Switch</h1>
+
+ <t:delegate to="case"/>
+
+ <t:block t:id="case1">
+ Here is the content for case1.
+ </t:block>
+
+ <t:block t:id="case2">
+ Here is the content for case2.
+ </t:block>
+
+ <t:block t:id="case3">
+ Here is the content for case3.
+ </t:block>
+
+ <t:block t:id="case4">
+ Here is the content for case4.
+ </t:block>
+ </body>
+</html>
+----
+
+ You can see, that the <<<Delegate>>> component's <<<to>>> parameter is bound
to the case property of your page class. In your
+ page class you therefore have a <<<getCase()>>> method that is responsible
for telling the <<<Delegate>>> component which
+ component should be rendered. For that we are injecting references to the
<<<Block>>>s defined in your page template
+ into the page class and return the according <<<Block>>> in the
<<<getCase()>>> method.
+
+----
+public class SwitchMe
+{
+ @Persist
+ private int whichCase;
+
+ @Inject
+ private Block case1, case2, case3, case4;
+
+ public Object getCase()
+ {
+ switch (whichCase)
+ {
+ case 1:
+ return case1;
+ case 2:
+ return case2;
+ case 3:
+ return case3;
+ case 4:
+ return case4;
+ default:
+ return null;
+ }
+ }
+}
+----
+
+ Happy switching!
+
Propchange: tapestry/tapestry5/trunk/src/site/apt/cookbook/switch.apt
------------------------------------------------------------------------------
svn:eol-style = native
Modified: tapestry/tapestry5/trunk/src/site/site.xml
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/src/site/site.xml?rev=769546&r1=769545&r2=769546&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/src/site/site.xml (original)
+++ tapestry/tapestry5/trunk/src/site/site.xml Tue Apr 28 21:20:24 2009
@@ -119,6 +119,7 @@
<item name="Exception Reporting" href="cookbook/exceptions.html"/>
<item name="Supporting Informal Parameters"
href="cookbook/informals.html"/>
<item name="Component Libraries" href="cookbook/lib.html"/>
+ <item name="Switching Cases" href="cookbook/switch.html"/>
</menu>
<menu name="Tapestry 5 Maven Support">