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

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

问题描述

我有一个关于组件 ID 和对话框(或其他组件)小部件变量名称的简单问题.

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

使用组件ID作为同一个组件的widget变量名有问题吗?

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

例如

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

推荐答案

这段 JSF 基本上生成以下 HTML(不完全相同,但效果相同):

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 对这种方法有问题.不明原因,污染全局 JavaScript 命名空间,通过它们的 ID(和名称)对所有 HTML 元素进行变量引用.因此,基本上,生成的 HTML 输出中的 var dlgRelConsultasRealizadas 是在渲染被

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:

上的 HTML 元素引用覆盖之后.就好像浏览器随后在全局范围内执行以下操作:

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.

这将导致所有原始小部件 var 函数完全不可用,因为变量 dlgRelConsultasRealizadas 现在正在引用 HTMLDivElement 实例,它没有与 show() 等原始小部件 var 相同的功能等

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

因此,建议为 widgetVar 指定一个唯一值,该值不用作任何(生成的)HTML 元素的 ID 或名称.一种常见的做法是使用一致的标签作为小部件变量名称的前缀(或后缀).例如.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.

<小时>

更新:自 PrimeFaces 4.0 起,出于上述原因以及第三方库"的全局命名空间污染"在 JavaScript 世界中被认为是不好的做法,不再注入小部件变量在全球范围内.从 PF4 开始,它们只能通过 PF() 函数使用.

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.

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

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