使用组件ID作为小部件变量名称 [英] Using the component ID as widgetVar name
问题描述
我有一个关于组件ID和对话框(或其他组件)小部件变量名称的简单问题.
使用组件ID作为同一组件的窗口小部件变量名称有问题吗?
例如
<p:dialog id="dlgRelConsultasRealizadas" widgetVar="dlgRelConsultasRealizadas" .../>
这段JSF基本上生成以下HTML(不完全相同,但效果相同):
<body>
<div id="dlgRelConsultasRealizadas">...</div> <!-- Component ID -->
<script>var dlgRelConsultasRealizadas = ...;</script> <!-- Widget var -->
</body>
Interner Explorer在使用此方法时遇到问题. 出于某些不清楚的原因,它污染了全局JavaScript名称空间,该变量具有通过其ID(和名称)对所有HTML元素的变量引用.因此,基本上,生成的HTML输出中的var dlgRelConsultasRealizadas
是在渲染后被<div>
上的HTML元素引用覆盖的.就像浏览器之后在全局范围内执行以下操作一样:
dlgRelConsultasRealizadas = document.getElementById("dlgRelConsultasRealizadas");
这将导致所有原始小部件var函数完全不可用,因为变量
更新:从PrimeFaces 4.0开始,出于上述原因以及第三方库"对全局名称空间的污染"在JavaScript世界中被视为不良做法,因此不再注入小部件变量在全球范围内.由于PF4仅可通过 换句话说 现在不再作为 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.
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 This will cause all original widget var functions to be completely unavailable because the variable Therefore, it's recommended to give the
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 In other words, is now not available anymore as 这篇关于使用组件ID作为小部件变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!dlgRelConsultasRealizadas
现在引用了
PF()
功能使用它们. <p:xxx ... widgetVar="foo">
foo
可用,而仅作为PF('foo')
可用.另请参见Hatam Alimam关于该主题的博客: PrimeFaces widgetVar简介.><p:dialog id="dlgRelConsultasRealizadas" widgetVar="dlgRelConsultasRealizadas" .../>
<body>
<div id="dlgRelConsultasRealizadas">...</div> <!-- Component ID -->
<script>var dlgRelConsultasRealizadas = ...;</script> <!-- Widget var -->
</body>
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");
dlgRelConsultasRealizadas
is now referencing a HTMLDivElement
instance which doesn't have the same functions as the original widget var like show()
, etc.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" .../>
PF()
function. <p:xxx ... widgetVar="foo">
foo
, but only as PF('foo')
. See also Hatam Alimam's blog on the subject: Intro to PrimeFaces widgetVar.