Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reworded intro paragraphs for simplicity, added an example, included build-in mixins section, added Thiago'

A Component Mixin is a way to supplement an existing Tapestry component with additional behavior.

Wiki Markup
{float:right|background=#eee}
{contentbylabel:title=Related Articles|showLabels=false|showSpace=false|space=@self|labels=component-classes,mixins}
{float}

Component Mixins

Tapestry 5 includes a radical feature, component mixins. Component mixins are a tricky concept; it basically allows a true component to be mixed together with special limited components called mixins. The component plus its mixins are represented as just a single tag in the component template, but all the behavior of all the elements.

...

You can think of a mixin as a kind of mashup for a component; it combines the behavior of the component mixin with the behavior of the mixincomponent, and bundles it all in one place. Mixins are used in two different scenarios: Instance mixins and Implementation mixinsmay be used to add validation to user input fields, or to add Ajax effects and behaviors to all sorts of components.

Tapestry comes with several mixins, such as the Autocomplete mixin which adds autocomplete behavior to an ordinary TextField Component. In addition, you can easily create your own.

Mixin Classes

Mixin classes are stored in a the mixins sub-package of your application, below the application (or library) root package. This parallels where your component and page classes are stored.

Other than that, mixin classes are exactly the same as any other component class.

...

Currently, mixins are allowed to do anything a component can do, including having parameters , and render phase methods.

Mixins may not have a template. They integrate with the component strictly in terms of invoking render phase methods.

...

Mixins may not, themselves, have mixins.

Using Mixins

Mixins are used in two different scenarios: Instance mixins and Implementation mixins.

Instance Mixins

An instance mixin is a mixin applied to a specific instance of a component. This can be done in the component template with the mixins attribute of the <comp> elementcomponent tag. This is a comma-separated list of mixin names.

Code Block
xml
xml

<t:textfield t:id="accountName" t:mixins="Autocomplete,DefaultFromCookie" />

Alternately, when the @Component annotation is used to define the component type, you may specify the mixins in two ways:

  • The @Mixins annotation allows a list of mixin names to be specified.
  • The @MixinClasses annotation allows a set of mixin class classes to be specified directly.

The former is often less verbose, and allows core mixins to be overridden with application-specific mixins. The later format is more specific and more

...

refactor-safe (renaming a mixin class will rename the entry in the MixinClasses annotation as well).

Example:

Code Block
java
java

  @Component(parameters=. . .) @Mixins({"Autocomplete", "DefaultFromCookie"})
  private TextField userId;

This example defines a component of type TextField and mixes in the hypothetical Autocomplete and DefaultFromCookie mixins.

Implementation Mixins

Implementation mixins, mixins which apply to all isntances instances of a component, are added using the @Mixin annotation. This annotation defines a field that will containg contain the mixin instance.

Code Block
java
java
public class AutocompleteField extendes TextField
{
  @Mixin
  private Autocomplete autocompleteMixin;
  
  . . .
}

...

Code Block
java
java
public class AutocompleteField extendes TextField
{
  @Mixin("Autocomplete")
  private Object autocompleteMixin;
  
  . . .
}

Note that Tapestry currently doesn't support parameters in implementation mixins.

Mixin Parameters

Mixins are allowed to have parameters, just like components.

When binding parameters (either in the template, or using the parameters attribute of the Component annotation)., Tapestry will match each parameter name against the parameters defined by each class (which is to say, the component and each mixin).

...

All mixins for a component execute their render phase methods before the component's render phase methods for most phases. However, in the later phases (AfterRender, CleanupRender) the order of executing is reversed.

Exception: Mixins A mixins whose class is annotated with @MixinAfter are is ordered after the component, not before.

Available Mixins

Include Page
TAPESTRY:Built-in Mixins
TAPESTRY:Built-in Mixins

In addition, the following mixins are available from other sources:

ClickOnce

From JumpStart, a mixin to apply to a submit button, ensuring it can't be double-clicked

Confirm

Adds a JavaScript confirm prompt to any link

Additional Tools

Tapestry-MixinsXpath is a third-part Tapestry module that allows XPath traversal of the Tapestry (server-side) DOM, which can be extremely useful in certain mixins.