You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by Apache Wiki <wi...@apache.org> on 2005/12/11 02:37:23 UTC

[Myfaces Wiki] Update of "JavascriptWithJavaServceFaces" by ChutYee

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Myfaces Wiki" for change notification.

The following page has been changed by ChutYee:
http://wiki.apache.org/myfaces/JavascriptWithJavaServceFaces

------------------------------------------------------------------------------
  The example below illustrates how the <h:commandLink> is rendered in html:
  
  -----------------------
+ {{{
  <h:form id=”userForm”>
  <h:commandLink id=”lnkDeleteUser” value=”delete” action=”#{userBean.deleteUser}”/>
  </h:form>
- ---------------------------------------------------------
+ }}}
+ 
+ {{{
  <a href="#" onclick=
  "clear_userForm();
- document.forms['userForm'].elements['userForm::_link_hidden_'].value='userForm::lnkDeleteUser';
+ document.forms['userForm'].elements['userForm:_link_hidden_'].value='userForm:lnkDeleteUser';
  if (document.forms['userForm'].onsubmit){
        if (document.forms['userForm'].onsubmit()) 
  document.forms['userForm'].submit();
@@ -31, +34 @@

  return false;
  " 
  id="userForm::lnkDeleteUser">delete</a>
+ }}}
- -------------------------------------------------------------------------------------
+ --------------------------------------------------------------------
+ 
  
  There are a few points to note:
  
@@ -44, +49 @@

  
  Below illustrates how to inject a line of code to open confirm dialog window, and the rendered HTML:
  
- ------------------------------------------------------------------------------------
+ ---------------------------------------------------------
+ {{{
  <h:form id=”userForm”>
      <h:commandLink id=”lnkDeleteUser” value=”delete” 
              onClick=”if (!confirm('Are you sure you want to delete this record?')) return false”
              action=”#{userBean.deleteUser}”/>
  </h:form>
- ---------------------------------------------------------
+ }}}
+ 
+ {{{
  <a href="#" onclick=
  "if (!confirm('Are you sure you want to delete this record?')) 
          return false; 
  clear_userForm();
- document.forms['userForm'].elements['userForm::_link_hidden_'].value='userForm::lnkDeleteUser';
+ document.forms['userForm'].elements['userForm:_link_hidden_'].value='userForm:lnkDeleteUser';
  if (document.forms['userForm'].onsubmit){
        if (document.forms['userForm'].onsubmit()) 
  document.forms['userForm'].submit();
@@ -65, +73 @@

  return false;
  " 
  id="userForm::lnkDeleteUser">delete</a>
+ }}}
  -------------------------------------------------------------------------------------
  
  Another point to note is that the javascript block should not return true under any circumstance. It is does so, the browser will proceed to perform <a href=”#”> – which is redirecting the browser to the dummy “#” page.
  
  == <h:commandButton> ==
  
- The command button is a little simpler. Below illustrates how to inject a confirmation dialog with <h:commandButton> and how it is rendered in HTML.
+ The command button is a little simpler. The script below illustrates how to inject a confirmation dialog with <h:commandButton> and how it is rendered in HTML.
  
  ---------------------------------------------------------
+ {{{
  <h:commandButton id=”btnCancel” value=”Cancel” 
        onclick=”if (!confirm('You will lose all changes made. Are you sure?')) return false"
  />
+ }}}
  -------------------------------------------------------
+ {{{
- <input id="userForm::btnCancel" name="userForm::btnCancel" 
+ <input id="userForm:btnCancel" name="userForm:btnCancel" 
        type="submit" value="Cancel" 
  onclick="
  if (!confirm('You will lose all changes made. Are you sure?')) return false;
  clear_userForm();
  "/>
+ }}}
- ------------------------------------------------------------
+ --------------------------------------------------------
  
  Here the commandButton is rendered as a HTML submit button. If the javascript block returns true then the form is submitted as usual. If it returns false then the form submission is aborted.
  
- The key to successful javascripting with JSF is to understand what is being rendered. Some basic understanding of javascipt goes a long way as well.
+ The key to successful javascripting with JSF is to understand what is being rendered. Some basic understanding of javascipt goes a long way as well. Also be extra careful with the syntax, as most IDE do not have any support for Javascript. If you make a syntax error - for example missing out a closing bracket, your code will simply fail silently.