Class Delegate


  • @SupportsInformalParameters
    public class Delegate
    extends Object
    A component that does not do any rendering of its own, but will delegate to some other object that can do rendering. This other object may be a component or a Block (among other things). This component may also be used to create inline components. For each informal parameter the value will be stored as a render variable. To create an inline component, create a block and use Delegate multiple times in the template to render the block passing parameters to Delegate. In the block body reference the render variables using the "var:" binding prefix and the name of the parameter. Note that the default binding prefix for informal parameter values is "literal".
    Component Parameters 
    NameTypeFlagsDefaultDefault Prefix
    toObjectRequired prop
    The object which will be rendered in place of the Delegate component. This is typically a specific component instance, or a Block.

    Examples

    The Delegate component allows us to be very flexible in how and what gets rendered, and in what order. In some cases, the object to be rendered may come from an entirely different page.

    This example is simpler, and could easily be accomplished using an If component. We'll create a page that can be used for viewing or editting an object.

    ViewAccount.tml

    <html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
        <body>
            <h1>View Account</h1>
    
            <t:delegate to="activeBlock"/>
    
            <t:block id="view">
                <t:beandisplay object="account"/>
    
                <p><t:actionlink t:id="edit">Edit this account</t:actionlink></p>
            </t:block>
    
            <t:block id="edit">
                <t:beaneditform t:id="account"/>
            </t:block>
        </body>
    </html>

    So we end up with a display of the Account's properties, and a link to activate edit mode. In edit mode, we use the other block and show a BeanEditForm.

    ViewAccount.java

    public class ViewAccount
    {
        @Persist
        private Account account;
    
        @Persist
        private boolean editMode;
    
        @Inject
        private Block edit, view;
    
        @Inject
        private AccountDAO accountDAO;
    
        public Account getAccount()
        {
            return account;
        }
    
        public void setAccount(Account account)
        {
            account = account;
            editMode = false;
        }
    
        void onSuccess()
        {
             accountDAO.update(_account);
    
            editMode = false;
        }
    
        void onActionFromEdit()
        {
            editMode = true;
        }
    
        public Object getActiveBlock()
        {
            return editMode ? edit : view;
        }
    }

    The use of the @Inject annotation on a field of type Block is used to access a <t:block> element from the template. The field name, stripped of leading underscores, is matched against the block's id.

    The rest is concerned with handling the form submission, turning on edit mode, and determining which block will be the one to render.