Creating Mixins

Mixins must reside under the mixins package.

Here we created a mixin called GridSortingDisabler. It is designed to mix in with a Grid, turning off the sorting of every column.
It works by modifying its Grid's BeanModel - after the Grid has prepared it but before the Grid is rendered.

IdVersionFirst NameLast NameRegionStart Date
4 26 yuno 3clover West Coast Feb 12, 2008

A mixin can work in many ways, for example:

References: Component Mixins, @Mixin, @MixinAfter, Grid, BeanModel, PropertyModel, DOM, MarkupWriter, Element.

Home

The source for IPersonFinderServiceLocal and @EJB is shown in the Session Beans and @EJB examples.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- We need a doctype to allow us to use special characters like &nbsp; 
     We use a "strict" DTD to make IE follow the alignment rules. -->

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_4.xsd">
<body class="container">
    <h3>Creating Mixins</h3>

    <p>
        Mixins must reside under the <code>mixins</code> package.
    </p>
    <p>
        Here we created a mixin called GridSortingDisabler. It is designed to mix in with a Grid, turning off the sorting of
        every column.<br /> It works by modifying its Grid's BeanModel - after the Grid has prepared it but before the Grid
        is rendered.
    </p>

    <div class="eg">
        <t:grid source="persons" rowsPerPage="4" pagerPosition="top" t:mixins="GridSortingDisabler">[Grid here]</t:grid>
    </div>

    <p>A mixin can work in many ways, for example:</p>
    <ul>
        <li>
            A mixin can <strong>manipulate its component's properties</strong>, like GridSortingDisabler which manipulates its
            Grid's BeanModel.
        </li>
        <li>
            A mixin can <strong>manipulate the DOM</strong>, like Tapestry's <a
                href="http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/RenderDisabled.java?view=markup">RenderDisabled</a>
            and <a
                href="http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/NotEmpty.java?view=markup">NotEmpty</a>,
            and nillehammer's <a
                href="http://tapestry.1045711.n5.nabble.com/Snippet-T5-A-mixin-to-rewrite-the-markup-of-element-td4479439.html#a4481772">MarkupChanger</a>.
        </li>
        <li>
            A mixin can <strong>bubble up events</strong>, like Tapestry's <a
                href="http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/mixins/RenderNotification.java?view=markup">RenderNotification</a>.
        </li>
        <li>
            A mixin can <strong>attach javascript</strong>, like the later examples "Create Mixins: ClickOnce", "AJAX Periodic
            Update Mixin" and "AJAX On Event: ZoneUpdater".
        </li>
    </ul>

    References:
    <a href="http://tapestry.apache.org/component-mixins.html">Component Mixins</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/annotations/Mixin.html">@Mixin</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/annotations/MixinAfter.html">@MixinAfter</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/Grid.html">Grid</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/beaneditor/BeanModel.html">BeanModel</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/beaneditor/PropertyModel.html">PropertyModel</a>,
    <a href="http://tapestry.apache.org/dom.html">DOM</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/MarkupWriter.html">MarkupWriter</a>,
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/dom/Element.html">Element</a>.
    <br />
    <br />

    <t:pagelink page="Index">Home</t:pagelink>
    <br />
    <br /> The source for IPersonFinderServiceLocal and @EJB is shown in the Session Beans and @EJB examples.
    <br />
    <br />

    <t:tabgroup>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/lang/CreatingMixins.tml" />
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/lang/CreatingMixins.java" />
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css" />
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/mixins/GridSortingDisabler.java" />
    </t:tabgroup>
</body>
</html>


package jumpstart.web.pages.examples.lang;

import java.util.List;

import javax.ejb.EJB;

import jumpstart.business.domain.person.Person;
import jumpstart.business.domain.person.iface.IPersonFinderServiceLocal;

import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.Property;

@Import(stylesheet = "css/examples/plain.css")
public class CreatingMixins {
    static private final int MAX_RESULTS = 30;

    // Screen fields

    @Property
    private List<Person> persons;

    // Generally useful bits and pieces

    @EJB
    private IPersonFinderServiceLocal personFinderService;

    // The code
    
    void setupRender() {
        // Get all persons - ask business service to find them (from the database)
        persons = personFinderService.findPersons(MAX_RESULTS);
    }
}


.eg {
                margin: 20px 0;
                padding: 14px;
                border: 1px solid #ddd;
                border-radius: 6px;
                -webkit-border-radius: 6px;
                -mox-border-radius: 6px;
}


//
// Based on an example by Steve Eynon posted in http://tapestry.1045711.n5.nabble.com/Grid-disable-sorting-mixin-td3401410.html .
//
package jumpstart.web.mixins;

import java.util.List;

import org.apache.tapestry5.annotations.InjectContainer;
import org.apache.tapestry5.annotations.MixinAfter;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.beaneditor.PropertyModel;
import org.apache.tapestry5.corelib.components.Grid;

@MixinAfter
public class GridSortingDisabler {

    @InjectContainer
    private Grid grid;

    void setupRender() {
        if (grid.getDataSource().getAvailableRows() == 0) {
            return;
        }

        BeanModel<?> gridBeanModel = grid.getDataModel();
        List<String> propertyNames = gridBeanModel.getPropertyNames();
        
        for (String propertyName : propertyNames) {
            PropertyModel propertyModel = gridBeanModel.get(propertyName);
            propertyModel.sortable(false);
        }
    }
}