1

I am using RichFaces 4. I am using flot as a JQuery Charting library and i want to implement a chart, that keeps refreshing itself via AJAX.

I get the data for the chart via JSON from my backing bean.

I am lost on how to get new data from a backing bean into a javascript variable. From what i understand i can't use <f:ajax> since there is no user interaction and no use of html input elements.

I think the solution would be the use of jsf.ajax.request. But i don't understand how to call that without an input-element either.

Following is a code snipped of how it should look like, but i don't know how to get the value from my backing bean into a javascript variable.

$(function() {
function update(){
    //**dont know how this call should look like**
    jsf.ajax.request('dev-form-input1', {render:'dev-form-output1'});
    //'newAjaxData' would be the value from #{someBean.chartData}
    plot.setData(newAjaxData);
    // draw the chart with the new data
    plot.draw();
    //get an update every second
    setTimeout(update, 1000);
}

I managed to send AJAX requests with jsf.ajax.request when using some input-element, but i didnt get the new value to render. And even if i could i wouldnt know hot to get it in a javascript variable (i dont have a lot of experience with javascript).

<h:form id="dev-form">
<h:inputText id="input1" value="#{someBean.chartData}"/>
<h:outputText id="output1" value="#{someBean.chartData}" />
</h:form>

note: i changed the javax.faces.SEPARATOR_CHAR to "-", because i need ":" for jquery.

Thanks for your help!

styx
  • 421
  • 8
  • 15
  • take a look at this http://stackoverflow.com/a/9886212/617373 – Daniel Jul 29 '12 at 14:32
  • 2
    JSF is the wrong tool for the job. Use a webservice framework like JAX-RS/WS, not a MVC framework like JSF. Then you can in the client end just use jQuery the usual way and so on. – BalusC Jul 29 '12 at 16:37
  • @Daniel Thanks, i will give this a try. I was hoping for a "cleaner" solution but am happy over every solution :) – styx Jul 30 '12 at 14:05
  • @BalusC This is an existing application i am working on, so i can't change the whole design. Also this is just a small part of the overall application. Anyway i thought that with jsf.ajax.request it's possible to do this without breaking too much with the JSF design, am i wrong? – styx Jul 30 '12 at 14:07
  • Uh, you don't need to change the whole design. You just need to change the not-working part. You should need to change it anyway even when going for some magic JSF compatible workaround/hack. Note that you shouldn't think that JSF/JAX-RS can't happily run next to each other in a single webapp. – BalusC Jul 30 '12 at 14:08
  • @BalusC I must admit i didnt even know JAX-RS. I should really look into all the JEE6 APIs, there seems to be a lot of cool stuff. Anyway, it was pretty straightforward to implement it with JAX-RS, so thanks again for your suggestion! I gave you an upvote. – styx Jul 30 '12 at 21:28
  • I reposted it as an answer so that you can accept it. – BalusC Jul 30 '12 at 21:34

1 Answers1

0

JSF is the wrong tool for the job. Use a webservice framework like JAX-RS, not a MVC framework like JSF. Then you can in the client end just use jQuery the usual way and so on.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555