Xavier Portebois
Xavier Portebois

Reputation: 3505

Is it possible to pass passthrough attributes in a custom composite component?

In JSF 2.2, I have a custom composite component which wraps a vanilla component (let's say a h:commandLink).

Is it possible to add passthrough attributes on my composite component and pass them back to the wrapped component?

Something like this (it's an example, don't bother about its uselessness):

Declaration of my:commandLink:

<cc:interface>
    <cc:attribute name="text" type="java.lang.String" />
</cc:interface>

<cc:implementation>
    <h:commandLink a:magicInsertionOfAllThePassThroughAttributes>
        #{cc.attrs.text}
    </h:commandLink>
</cc:implementation>

Use of my:commandLink:

<my:commandLink text="oh no" a:data-something="…" a:data-otherthing="…" />

Upvotes: 0

Views: 1328

Answers (2)

tandraschko
tandraschko

Reputation: 2346

I think the JSF API is missing a helper for this. Maybe something like "insertPassthroughAttributes", similar like cc:insertChildren. Maybe OmniFaces can prototype this and we can add it to the next JSF specs. Someone should create a spec issue.

Upvotes: 1

Xavier Portebois
Xavier Portebois

Reputation: 3505

So, following @Kukeltje comments, I managed to do something with a custom composite component backing class:

@FacesComponent("PassThrough")
public class PassThroughComponent extends Component
{
    @Override
    public void encodeBegin(FacesContext facesContext) throws IOException
    {
        super.encodeBegin(facesContext);
        findComponent("pass").getPassThroughAttributes().putAll(getPassThroughAttributes());
    }
}

With a DOM element bearing a id='pass' in my component, I can pass the attributes to it.

Alas, Eclipse doesn't understand the trick, and triggers warnings seeing all these "unknown attribute 'a:something'".

I truly wonder how Primefaces components manage to avoid that.

Upvotes: 2

Related Questions