使用组件ID作为小部件变量名称 [英] Using the component ID as widgetVar name

查看:141
本文介绍了使用组件ID作为小部件变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于组件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函数完全不可用,因为变量dlgRelConsultasRealizadas现在引用了


更新:从PrimeFaces 4.0开始,出于上述原因以及第三方库"对全局名称空间的污染"在JavaScript世界中被视为不良做法,因此不再注入小部件变量在全球范围内.由于PF4仅可通过PF()功能使用它们.

换句话说

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

现在不再作为foo可用,而仅作为PF('foo')可用.另请参见Hatam Alimam关于该主题的博客: PrimeFaces widgetVar简介.

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" .../> 

解决方案

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.

这篇关于使用组件ID作为小部件变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆