2 Replies Latest reply on Mar 12, 2008 11:15 AM by stefan r

    HtmlAjaxFunction data

    stefan r Newbie

      Hello
      i am trying to create a jsf component which uses HtmlAjaxFunction as a child component.

      I tested my component and it works if i put the <a4j:jsFunction/> in the JSF.

      
      <a:jsFunction name="jsTestAction"
      action="#{TeamWerkstattTextsJsfBean.loadTextList}"
      data="#{TeamWerkstattTextsJsfBean.listXml}"
      oncomplete="fillGrid(data)">
      </a:jsFunction>
      

      The above jsFunction works this way:
      1) on window load jsTestAction is called
      2) loadTextList loads some xml data into listXml
      3) oncomplete the js function fillGrid(data) is called and i do get the content of listXml into data

      now i want to put this into my Component Renderer. I tried this:

      ...
      HtmlAjaxFunction myFunction = new HtmlAjaxFunction();
       ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
       ELContext elCtx = context.getELContext();
       MethodExpression actionExpression = expressionFactory.createMethodExpression(elCtx, "#{TeamWerkstattTextsJsfBean.loadTextList}", null, new Class[]{});
       ValueExpression dataExpression = expressionFactory.createValueExpression(elCtx, "#{TeamWerkstattTextsJsfBean.listXml}", String.class);
      
       myFunction.setName("jsDhtmlxLoadListXml");
       myFunction.setActionExpression(actionExpression);
       myFunction.setOncomplete("fillGrid(data)");
       myFunction.setParent(component.getParent());
       myFunction.setData(dataExpression);
      
       myFunction.encodeBegin(context);
       myFunction.encodeEnd(context);
      
      
       writer.startElement("script", null);
       writer.writeText("window.addEventListener('load',function(evt){alert('onload calling...'); jsDhtmlxLoadListXml();},false);",null);
       writer.endElement("script");
      
      ...
      


      ... but i do not get data in my fillGrid js function any more.
      i also tried
      myFunction.setData("#{TeamWerkstattTextsJsfBean.listXml}");
      


      that didnt work either.

      So my question is, how to create the <a4j:jsFunction> written above programatically in my Renderer, in particular with data attribute set?

      I am using latest snapshot: richfaces-ui-3.2.0-20080312.044927-95

      thanks a lot for helping!

      stefan.

      ps: btw, greate piece of software! thanks!

        • 1. Re: HtmlAjaxFunction data
          Fab Mars Novice

          Having done a couple of jsf component, one basic rule is you shouldn't play with save/restorestate and you shouldn't play with encode methods.

          Here you aren't supposed to
          - add components whereas you're in the renderer because you cannot guarantee the jsf tree state anymore
          - trigge children components' encodeBegin and encodeEnd.

          • 2. Re: HtmlAjaxFunction data
            stefan r Newbie

            hello fabmars,

            thank you for your your tips!
            indeed i am new to jsf, components, richfaces, etc. this is my first project and it really makes fun...

            i followed this http://cagataycivici.wordpress.com/2005/12/13/dynamically_adding_components_to_a/
            article when i realized that my component actually consists of several "subcomponents" that are supposed to work together.

            so, if this is not the (best) way to do it, how can i add this functionality to my component?
            what i want my component to do is
            1) render some html + jscript
            2) attach a windows.onload listener that triggers a jsf bean action
            3) on complete of 2) give some jsf bean property back to clientside
            4) use jsf bean property to finish rendering my component

            actually 2+3 is exactly what jsFunction is for, thats why including HtmlAjaxFunction into my component appeard to be straight forward.


            BTW, my jsf component should wrap around this:
            http://www.dhtmlx.com/docs/products/dhtmlxGrid/index.shtml


            thanks for your help!
            BR
            stefan.