12

I have a simple question about component IDs and dialog (or other components) widget variable name.

Is there a problem of using the component ID as widget variable name of the same component?

E.g.

<p:dialog id="dlgRelConsultasRealizadas" widgetVar="dlgRelConsultasRealizadas" .../> 
3
  • im asking if there is a problem doing it? i've see on some forum (i think is primefaces forum) that doing that is not correct, and i wanna know ur opinion about that.
    – Cold
    Sep 30, 2013 at 13:45
  • Thanks for ur attention, i found and explanation here link. Moderator can close this post.
    – Cold
    Sep 30, 2013 at 13:53
  • I guess you can post it as your answer so that others can benefit Sep 30, 2013 at 14:00

1 Answer 1

13

This piece of JSF generates basically the following HTML (not exactly that, but in effects the same):

<body>
    <div id="dlgRelConsultasRealizadas">...</div> <!-- Component ID -->
    <script>var dlgRelConsultasRealizadas = ...;</script> <!-- Widget var -->
</body>

Interner Explorer has problems with this approach. For some unclear reason, it pollutes the global JavaScript namespace with variable references to all HTML elements by their ID (and name). So, basically, the var dlgRelConsultasRealizadas from the generated HTML output is after the rendering been overridden by the HTML element reference on the <div>. It's like as if the browser is afterwards doing the following in the global scope:

dlgRelConsultasRealizadas = document.getElementById("dlgRelConsultasRealizadas");

This will cause all original widget var functions to be completely unavailable because the variable dlgRelConsultasRealizadas is now referencing a HTMLDivElement instance which doesn't have the same functions as the original widget var like show(), etc.

Therefore, it's recommended to give the widgetVar an unique value which is not been used as ID or name of any (generated) HTML element. A common practice is to prefix (or suffix) the widget variable name with a consistent label. E.g. the w_.

<p:dialog id="dlgRelConsultasRealizadas" widgetVar="w_dlgRelConsultasRealizadas" .../> 

Update: since PrimeFaces 4.0, for among others the above reason and also because "global namespace pollution" by "3rd party libraries" is considered bad practice in JavaScript world, widget variables are not anymore injected in the global scope. They are since PF4 only available via the PF() function.

In other words,

<p:xxx ... widgetVar="foo">

is now not available anymore as foo, but only as PF('foo'). See also Hatam Alimam's blog on the subject: Intro to PrimeFaces widgetVar.

3
  • Good explanantion, its better than my serachs. Thanks ^^
    – Cold
    Sep 30, 2013 at 14:33
  • Chrome does that also AFAIK. As long as the id is withing naming container, everything will be OK, so it's a kind of forgotten landmine bug (it explodes once you've forgotten where you've placed it) Sep 30, 2013 at 14:58
  • You're welcome. By default (with right doctype, decent JS code, etc), Chrome isn't that dumb as IE that it completely overrides the original variable if already definied before.
    – BalusC
    Sep 30, 2013 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.