Using the component ID as widgetVar name

前端 未结 1 1247
长发绾君心
长发绾君心 2020-12-31 11:16

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 n

1条回答
  •  心在旅途
    2020-12-31 12:09

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

    
        
    ...

    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

    . 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_.

     
    

    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,

    
    

    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.

    0 讨论(0)
提交回复
热议问题