Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't stop primefaces poll

I'm implementing some dialogs that need a common poll to get fresh values from the server. I'm trying use p:poll, but unfortunately I cant stop it. I start the poll when a user clicks on a button in one dialog, and try stop that when a user clicks a button in a child dialog. This is the code I`m using to start and stop the poll:

firstDialog:

<p:poll autoStart="false" widgetVar="pollQtdDisponivelCarregamento" immediate="true"
    update="labelQuantidadeDisponivelCarregamento labelQuantidadeDisponivelItem"
    listener="#{atualizadorQuantidadeDisponivelProduto.atualizarQuantidadeDisponivel(modeloPopupCarregarProduto.produtoSelecionado)}" />
<p:commandButton action="#{controladorPopupCarregarProduto.abrir}"
    value="#{vendaMsg['popup.pre_venda.botao.adicionar_produto']}"
    title="#{vendaMsg['popup.pre_venda.botao.adicionar_produto.descricao']}"
    update="@form" onclick="pollQtdDisponivelCarregamento.start()" />

childDialog:

<p:commandButton value="OK" style="float:right" immediate="true"
    action="#{controladorPopup.fechar}" update="@form"
    onsuccess="pollQtdDisponivelCarregamento.stop();" />

One thing I can't understand is: when I break javascript execution using Firebug debug, the poll stops correctly, but when I don't do this, it just don't stop. Someone knows how can I solve this??

like image 842
brevleq Avatar asked Nov 18 '11 12:11

brevleq


1 Answers

I had similar problems with the 'poll' component (google sent me here). As far as I can tell there are two problems with your markup. I was using Primefaces 3.0M4.

Problem 1

Using <p:poll clientVar="myPoll"/> will create a javascript object accessible using window.myPoll. If the containing form is rerendered (see your 'update' attribute) this property is overwritten. The problem is that uses 'window.setInterval' under the hood. If the poller isn't stopped before beeing replaced this interval is lost.

Solution:

  1. You may create a top-level <form> containing the poll component only.
  2. onclick="pollQtdDisponivelCarregamento.start(); return false;" prevents the form from being sent to the server. (I doubt this is your intention)

Problem 2

poll.start() doesn't check if it's already running. Invoking it twice will cause another interval to be scheduled. The first one's id is lost, but it's still there sending requests to your server. Try clicking the button in 'first dialog' rapidly.

Solution:

Check if it's already running by calling poll.isActive() first.

like image 188
MartinK Avatar answered Sep 30 '22 06:09

MartinK