joi, 1 octombrie 2015

Access a component by using a reference to it

When you need to refer a component from another component, you can use the binding attribute, as below - amongst other things, this attribute is also used to bind the corresponding component to the current view, without the need of a bean (binding to a bean is not even recommended - Bauke Scholtz (aka BalusC) provide a comprehensive explanation on StackOverflow). This is useful to gain access to the state of a component from another component. Binding a component without a bean property will put the component in the EL scope:

<h:selectBooleanCheckbox id="nameId" label="name" binding="#{name}"/> Name
<h:inputText rendered="#{name.value}" a:placeholder="#{name.label}"/>

A great alternative to this approach consist in using the OmniFaces, ResolveComponent.  This component is capable of creating a reference for a component indicated by ID. Moreover, the reference is stored by default in the ʺfacelet scopeʺ but it can be alternatively stored in the request scope (this option can be set via the optional attribute, scope). The name attribute is required and it represents the name under which the referenced component will be made available to EL. The for attribute is also required and it represents the ID of the referenced component.

<h:selectBooleanCheckbox id="nameId" label="name"/> Name
<o:resolveComponent name="nameRef" for="nameId" />
<h:inputText rendered="#{nameRef.value}" a:placeholder = "#{nameRef.label}"/>

Check OmniFaces Showcase for more details.

Niciun comentariu:

Trimiteți un comentariu