4 Replies Latest reply on Aug 5, 2009 10:06 AM by zjda

    user a4j:jsFunction to save rich:editor content

    zjda

      Hi, I have an a4j:commandButton to close a rich:editor. Before closing the editor, I want to display a dialog to ask if the user wants to save the modified content. The following is my simplified code:

      <script type="text/javascript">
      function saveContent() {
       if (tinyMCE == null || tinyMCE.activeEditor == null || tinyMCE.activeEditor.getDoc() == null) {
       return;
       }
       if (tinyMCE.activeEditor.isDirty()) {
       var answer = confirm("The content has been changed. Do you want to save?");
       if (answer == true) {
       tinyMCE.triggerSave();
       saveData();
       }
       }
      }
      </script>
      
      <a4j:form ajaxSubmit="true" style="margin:0px; padding:0px;" >
      <a4j:jsFunction action="#{contentView.testSave}" name="saveData" ajaxSingle="true" eventsQueue="contentQueue" oncomplete="alert('completed');"/>
      
      <a4j:region>
       <a4j:commandButton value="Close"
      styleClass="content_button"
      disabled="#{contentView.openedNode.editorViewMode}"
      action="#{contentView.openedNode.closeAction}"
      rendered="#{contentView.openedNode.htmlContent}"
      onclick="saveContent();" reRender="mainFrame">
       </a4j:commandButton>
      </a4j:region>
      
      <rich:editor id="htmlEditor" value="#{contentView.contentString}" theme="advanced"
      readonly="#{contentView.openedNode.editorViewMode}"
      rendered="#{contentView.openedNode.htmlContent}">
      </rich:editor>
      </a4j:form>
      


      The saveData function is called as testSave() action is invoked. However, setcontentString() are not called and modified content is not saved. If I remove <a4j:region> tag around the a4j:commandButton, the content will be saved everytime I click on the button.

      Any help will be appreciated,
      -ZJ

        • 1. Re: user a4j:jsFunction to save rich:editor content
          yyq2009

           


          The saveData function is called as testSave() action is invoked. However, setcontentString() are not called and modified content is not saved.


          This is because you set a4j:jsFunction attribtue "ajaxsingle='true'", if you want to pass the editor's value to server, you should add attribute "process='htmlEditor'" to it.


          If I remove <a4j:region> tag around the a4j:commandButton, the content will be saved everytime I click on the button.


          I don't think it will run like your description, maybe, you close action do the save action again.
          My opinion only!!

          • 2. Re: user a4j:jsFunction to save rich:editor content
            zjda

            Thank you for your reply.

            As you pointed it out, "ajaxsingle='true'" caused the problem. After I remove the attribute, it works as expected even without attribute "process='htmlEditor'".

            My close action does not save it. I think that the close button submits the form, the server processes the editor component and save the content if there is no a4j:region around it.

            Regards,
            -ZJ

            • 3. Re: user a4j:jsFunction to save rich:editor content
              yyq2009

              That's right, if you remove "ajaxsingle=true", it works normally, but, if you do this, the whole form will be submitted as well, it means that you form has four fields, then four fields's value will be transfered to the server. if you just want to use editor's value, you should add "ajaxsingle=true" and "ajaxsingle=true", then only the editor's value will pass to the server.


              I think that the close button submits the form


              That's true.

              I don't know your words "the content will be saved everytime" means, your "saved" means server cant read the eidtor's value or the value saved into DB or some other meaning?

              • 4. Re: user a4j:jsFunction to save rich:editor content
                zjda

                Yes, "saved" means setContentString() is called which saved the value in the editor to the DB. Now I put a4j:region around jsFunction and the editor, and I think only these two components are processed when the jsFucntion is invoked.

                Thanks,
                -ZJ