I ran into an issue where i had the same id for both the id attribute and widgetvar, Apparently it is not recommended to have the same value for both attributes. so i am trying to find whats the best convention to follow. Are there special (reserved) characters that cannot be used like a period etc. Thanks
Asked
Active
Viewed 2,480 times
1 Answers
2
Just use a specific prefix consistently. There's not really a "best" prefix nor an officially recommended convention. The "best" is just to be consistent with it throughout the entire web application.
E.g.
<p:tabView id="tabs" widgetVar="$tabs">
<p:dataTable id="table" widgetVar="$table">
<p:calendar id="calendar" widgetVar="$calendar">
or
<p:tabView id="tabs" widgetVar="_tabs">
<p:dataTable id="table" widgetVar="_table">
<p:calendar id="calendar" widgetVar="_calendar">
or
<p:tabView id="tabs" widgetVar="p_tabs">
<p:dataTable id="table" widgetVar="p_table">
<p:calendar id="calendar" widgetVar="p_calendar">
Note that $
is disallowed in JSF client IDs.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
-
Won't the `$` as a widgetVar be 'dangerous'? PrimeFaces ships with jQuery and using `$` invokes jQuery. – siebz0r Sep 12 '12 at 15:09
-
1@siebz0r: That's not the same as `$()` function. It's just a variable name. The `$tabs` doesn't do magically `$('tabs')` or something. By the way, it's in jQuery the standard variable name convention for any jQuery objects. In this particular PrimeFaces widgetVar case it's not exactly the same, but it's just to illustrate the idea. – BalusC Sep 12 '12 at 15:09
-
That is certainly true and I don't have nearly as much experience with JSF as you, but is using a `$` as a prefix common? It would confuse me at first when I would see something like `$dialog.show()` or similar. – siebz0r Sep 12 '12 at 15:14
-
2@siebz0r: http://stackoverflow.com/questions/1180213/the-dollar-sign The average JS developer would treat `var tabs = document.getElementById("tabs")` as a plain HTML element and `var $tabs = $("#tabs")` as a jQuery object and would thus safely assume that all jQuery specific functions are available on it (note that it's not JSF specific but JS/jQuery specific). – BalusC Sep 12 '12 at 15:14
-
Thanks guys for the feedback, i have used a constant with an "_" (underscore) as my convention – Gowtham Sep 12 '12 at 15:18