jsfprimefacesclientid

Difference between referencing component with client ID and using p:component


I'm currently changing the structure of one of my primefaces 6.1 based project's core subviews for a Proof of Concept and found lots of references like X:Y:Z:A, X:Y:Z:B, etc... that were obviously failing after moving the referenced components elsewhere in the tree so my solution was to replace the "long id path" by p:component('A') and so on. After several replacements some questions came to my mind: "Why didn't they just use p:component()?", "Were them intentionally referred this way...if so why?". And finally the most important - THIS. Is there any difference between calling a component with its ID and its ancestors like X:Y:Z:A and using p:component('A')??

Considering also the following scenario... what would p:component('x') return? Would there be any collision or ambiguity exception?

 <f:view>
    <h:outputText id="x" />
    <h:form id="form1">
      <h:outputText id="x" />
    </h:form>
  </f:view>

Thanks in advance


Solution

  • A few things...

    1. p:component find the "first" reference it can find and returns it so I believe it would find the first h:outputText.

    2. p:component has been deprecated and is has been removed for the upcoming PF 7.1 release you can see here in the Migration Guide notes: https://github.com/primefaces/primefaces/wiki/Migration-Guide

    So to answer you are better off either referencing it directly with :form1:x or if you want p:component behavior where it find the first you can use PFS Selector Framework @root:@id(x) See examples here: https://www.primefaces.org/showcase/ui/ajax/selector.xhtml