4

In order to refresh data, I use a primefaces poll:

<p:poll id="myPoll" interval="#{controller.interval}"/>

and would like to control the update interval with a spinner.

<p:spinner  value="#{controller.interval}">
  <p:ajax process="@this" update="myPoll"/>
</p:spinner>

The value of the bean property is updated as expected, but the polling interval isn't. The polling is performed by a generated script, which includes the interval value. How do I force the poll to regenerate the update script to include the new value without submitting/rerenderng the whole form?

kostja
  • 60,521
  • 48
  • 179
  • 224

2 Answers2

4

If you give it a widgetVar then you can manipulate it with javascript.

widget_form_j_idt19.stop();
widget_form_j_idt19.timer = 1;
widget_form_j_idt19.cfg.frequency = 1;
widget_form_j_idt19.start();

Just put it in a script and you should be able to update it to whatever you want after an ajax update.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
  • 1
    Thanks, maple_shaft. Took me a while to try it out, but it's actually working. theres sth I dont quite get though - The `timer` is a function in the original pf.js. Why does setting it to a number work? And - when I examine the elements with firebug, the script for the poll element does not change. Why? – kostja Jun 18 '12 at 07:23
3

Solved here:

I was facing to similar behavior which is solved just setting the values in the bean and embedding the poll in a panel which can be updated eg after every poll. Otherwises, poll is just js code, and not added in the DOM. Otherwise you may play around executing js on callback parameters and refreshing the values on the Client Side.

    <p:outputPanel id="infoPanel" >
    <h:panelGrid columns="1">
        <h1>TESTING POLLING</h1>
        <h:outputText id="ts" value="TS Update:#{testPollBean.timeStamp}"/>
        <h:outputText id="callNum" value="Call Number:#{testPollBean.callNumber}°"/>
        <h:outputText id="interval" value="Interval:#{testPollBean.interval} second"/>
        <h:outputText id="Stop" value="Stop:"#{testPollBean.stopPolling}"/>
    </h:panelGrid>  
</p:outputPanel>

<h:panelGrid id="panelPolling">
    <p:poll id="durrPolling" async="true" stop="#{testPollBean.stopPolling}"
        interval="#{testPollBean.interval}" 
        process="@this" partialSubmit="true" listener="#{testPollBean.listener}"
        update="panelPolling infoPanel" widgetVar="polling"
        autoStart="true" rendered="true"/>
</h:panelGrid>

@ViewScoped
@Named(value = "testPollBean")
public class TestPollBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(TestPollBean.class);
    private Calendar calendar;
    private Date timeStamp;
    private Date nextCall;
    private int callNumber;
    private int interval;
    private boolean stopPolling;
    private boolean listenerBusy;

    @PostConstruct
    public void init() {
        this.calendar = Calendar.getInstance();
        this.timeStamp = calendar.getTime();
        this.nextCall = null;
        this.callNumber = 0;
        this.interval = 1;
        stopPolling = false;
        listenerBusy = false;
    }

    public void listener() {
        callNumber ++;
        LOG.info("-> Poll "+ callNumber + "° on thread " + Thread.currentThread().getId());
        if (!listenerBusy) {
            listenerBusy = true; // skip any other concurrence call 

            if (callNumber >= 30) {
                //stopPolling = true;
                this.interval = 30;
            }

            if (callNumber < 20) {
                this.timeStamp = calendar.getTime();
                calendar.add(Calendar.SECOND, interval);
                nextCall = calendar.getTime();

                LOG.info("\t > Attennding "+ callNumber + "° call on  thread " + Thread.currentThread().getId() + ". Sleeping ..." );
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    LOG.info("\t\t E R R O R ! ! ! \t > Attennding "+ callNumber + "° call on  thread " + Thread.currentThread().getId() );
                }
                //Later on we will try to adapt dynamically the interval.
            } else  {
                LOG.info("\t > Attennding inmediately "+ callNumber + "° call on  thread " + Thread.currentThread().getId() + "." );
            }
            LOG.info("<-  Poll "+ callNumber + "° on thread " + Thread.currentThread().getId());

            listenerBusy = false;
        } else {
            LOG.info("<- Listener on thread " + Thread.currentThread().getId() + " dismiss call " + callNumber + "° cause is already busy.");
        }
    }
    + Getters / Setters

IMHO; don't play around with something which is done and working.